class TinggEncryption::Encryption
@!parse
Public Class Methods
new(secret_key, iv_key)
click to toggle source
# File lib/tinggEncryption.rb, line 14 def initialize(secret_key, iv_key) if secret_key.nil? || (secret_key.size != 32) \ && iv_key.nil? || iv_key.nil? || iv_key.size != 16 raise 'Key Error' end @algorithm = 'AES-256-CBC' # Create a encoded key and secret # We could also have just created a random key @secret_key = Digest::SHA2.hexdigest(secret_key)[0..31] # By default data is padded to the nearest 16 bytes block. To turn # this off, you may use the :padding => false (or nil) option. # # In this mode however, the caller is required to pad the data. In # the following example the message is exactly 16 bytes long, so no # error aries. @iv_key = Digest::SHA2.hexdigest(iv_key)[0..15] end
Public Instance Methods
encrypt(data)
click to toggle source
# File lib/tinggEncryption.rb, line 34 def encrypt(data) key = @secret_key iv_key = @iv_key # @type raise 'Key Error' if key.nil? && iv_key.nil? # encryption_type encryption = OpenSSL::Cipher.new(@algorithm) encryption.encrypt encryption.key = key encryption.iv = iv_key cypher = encryption.update(JSON(data)) cypher << encryption.final Base64.encode64(Base64.encode64(cypher)) end