Class: TinggEncryption::Encryption
- Inherits:
-
Object
- Object
- TinggEncryption::Encryption
- Defined in:
- lib/tinggEncryption.rb
Instance Method Summary collapse
- #encrypt(data) ⇒ Object
-
#initialize(secret_key, iv_key) ⇒ Encryption
constructor
A new instance of Encryption.
Constructor Details
#initialize(secret_key, iv_key) ⇒ Encryption
Returns a new instance of Encryption.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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 |
Instance Method Details
#encrypt(data) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# 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 |