class Hide::AE

AE implements authenticated encryption API based on AES-256

Public Class Methods

decrypt( data, key, salt, iter, iv, auth_tag, auth_data = "", key_length = 32 ) click to toggle source

Decrypts an encrypted datastream with authenticity verification check

Returns the decrypted data

   # File lib/hide/ae.rb
30 def decrypt(
31   data, key, salt, iter, iv, auth_tag, auth_data = "", key_length = 32
32 )
33   decipher = OpenSSL::Cipher.new "aes-256-gcm"
34   decipher.decrypt
35   decipher.key =
36     OpenSSL::PKCS5.pbkdf2_hmac_sha1(key, salt, iter, key_length)
37   decipher.iv = iv
38   decipher.auth_tag = auth_tag
39   decipher.auth_data = auth_data
40   decipher.update(data) + decipher.final
41 rescue OpenSSL::Cipher::CipherError
42   raise ArgumentError, "Authentication failed"
43 end
encrypt( data, key, salt, iter, iv = SecureRandom.random_bytes(12), auth_data = "", key_length = 32 ) click to toggle source

Encrypts a data stream with an authenticity tag for reliable decryption

Returns a hash containing encrypted data, IV and authentication tag

   # File lib/hide/ae.rb
 8 def encrypt(
 9   data, key, salt, iter, iv = SecureRandom.random_bytes(12),
10   auth_data = "", key_length = 32
11 )
12   cipher = OpenSSL::Cipher.new "aes-256-gcm"
13   cipher.encrypt
14   cipher.key =
15     OpenSSL::PKCS5.pbkdf2_hmac_sha1(key, salt, iter, key_length)
16   cipher.iv = iv
17   cipher.auth_data = auth_data
18   {
19     data: cipher.update(data) + cipher.final,
20     iv: iv,
21     auth_tag: cipher.auth_tag
22   }
23 end