class TinggEncryption::Encryption
This is the Main class
Public Class Methods
new(iv_key, secret_key)
click to toggle source
# File lib/tingg_encryption.rb, line 13 def initialize(iv_key, secret_key) # We use the AES 256 bit cipher-block chaining symmetric encryption @algorithm = 'AES-256-CBC' raise 'Key Error' if secret_key.nil? || (secret_key.size >= 32) # Create a encoded key and secret # We could also have just created a random key # key = OpenSSL::Cipher::Cipher.new(alg).random_key @encoded_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. @encoded_iv = Digest::SHA2.hexdigest(iv_key)[0..15] end
Public Instance Methods
encryption(raw_data)
click to toggle source
# File lib/tingg_encryption.rb, line 33 def encryption(raw_data) # @type raise 'Key Error' if @encoded_key.nil? || @encoded_key.size != 32 string = raw_data.to_s # Now we do the actual setup of the cipher encryptor = OpenSSL::Cipher.new(@algorithm) encryptor.encrypt encryptor.key = @encoded_key encryptor.iv = @encoded_iv # Now we go ahead and encrypt our plain text. encryption = encryptor.update(string) encryption << encryptor.final Base64.urlsafe_encode64(Base64.urlsafe_encode64(encryption)) end