Module: Aes

Included in:
PasswordManager
Defined in:
lib/lockr/encryption/aes.rb

Instance Method Summary collapse

Instance Method Details

#decrypt(string, pass, salt) ⇒ Object

decrypt the string with AES 256-bit CBC encryption. the key and iv are calculated by the derive_key_iv method using the provided password.



24
25
26
27
28
29
30
31
32
33
# File 'lib/lockr/encryption/aes.rb', line 24

def decrypt( string, pass, salt)
  key, iv = derive_key_iv( pass, salt)
  
  decipher = OpenSSL::Cipher::AES.new(256, :CBC)
  decipher.decrypt
  decipher.key = key
  decipher.iv = iv

  decipher.update( string) + decipher.final
end

#derive_key_iv(pass, salt) ⇒ Object

derive a key and initial vector from the password thru the use of PKCS5 pbkdf2 key derivation function.



37
38
39
40
41
42
# File 'lib/lockr/encryption/aes.rb', line 37

def derive_key_iv( pass, salt)
  key = OpenSSL::PKCS5::pbkdf2_hmac_sha1( pass, salt, 4096, 32)
  iv = OpenSSL::PKCS5::pbkdf2_hmac_sha1( pass, salt, 4096, 16)
  
  [key, iv]
end

#encrypt(string, pass) ⇒ Object

encrypt the string with AES 256-bit CBC encryption. the key and iv are calculated by the derive_key_iv method using the provided password.

returns encrypted_string, salt



10
11
12
13
14
15
16
17
18
19
# File 'lib/lockr/encryption/aes.rb', line 10

def encrypt( string, pass)
  salt = SecureRandom.random_bytes(16)
  key, iv = derive_key_iv( pass, salt)
  
  cipher = OpenSSL::Cipher::AES.new(256, :CBC)
  cipher.encrypt
  cipher.key = key
  cipher.iv = iv
  [cipher.update(string) + cipher.final, salt]
end