Sha256: fa0ac0bfb6bfec235bfae03617204159011486ffd25407df75743d813a4c9c53

Contents?: true

Size: 1.07 KB

Versions: 10

Compression:

Stored size: 1.07 KB

Contents

# coding: utf-8
module RockFintech
  module Encrypt
    module RSA
      MAX_ENCRYPT_LENGTH = 245
      MAX_DECRYPT_LENGTH = 256

      def self.encrypt(content, public_key)
        content_str, encryt_str, offset, i = "", "", 0, 0

        bytes_array = content.unpack("C*")
        input_length = bytes_array.length
        while  input_length - offset > 0
          encryt_bytes = bytes_array[offset, MAX_ENCRYPT_LENGTH]
          encryt_str << public_key.public_encrypt(encryt_bytes.pack("C*"))
          offset = (i += 1) * MAX_ENCRYPT_LENGTH
        end
        Base64.strict_encode64(encryt_str)
      end

      def self.decrypt(content, private_key)
        result_str, decryt_bytes, offset, i = "", "", 0, 0

        content_str = Base64.strict_decode64(content)
        input_length = content_str.length
        while input_length - offset > 0
          decryt_bytes = content_str[offset, MAX_DECRYPT_LENGTH]
          result_str << private_key.private_decrypt(decryt_bytes)
          offset = (i += 1) * MAX_DECRYPT_LENGTH
        end
        result_str
      end

    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rock_fintech-0.19.0 lib/rock_fintech/encrypt/rsa.rb
rock_fintech-0.18.4 lib/rock_fintech/encrypt/rsa.rb
rock_fintech-0.18.1 lib/rock_fintech/encrypt/rsa.rb
rock_fintech-0.17.0 lib/rock_fintech/encrypt/rsa.rb
rock_fintech-0.16.0 lib/rock_fintech/encrypt/rsa.rb
rock_fintech-0.15.0 lib/rock_fintech/encrypt/rsa.rb
rock_fintech-0.14.0 lib/rock_fintech/encrypt/rsa.rb
rock_fintech-0.13.0 lib/rock_fintech/encrypt/rsa.rb
rock_fintech-0.12.0 lib/rock_fintech/encrypt/rsa.rb
rock_fintech-0.11.0 lib/rock_fintech/encrypt/rsa.rb