Class: DbchainClient::AESCrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/dbchain_client/aes.rb

Instance Method Summary collapse

Constructor Details

#initializeAESCrypt

Returns a new instance of AESCrypt.



7
8
9
# File 'lib/dbchain_client/aes.rb', line 7

def initialize
  @cipher = OpenSSL::Cipher.new('AES-256-CBC')
end

Instance Method Details

#decrypt(password, secret_data_with_iv) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/dbchain_client/aes.rb', line 23

def decrypt(password, secret_data_with_iv)
  iv_encrypted = Base64::decode64(secret_data_with_iv)
  iv, encrypted = extract_iv_and_encrypted(iv_encrypted)

  @cipher.decrypt
  @cipher.key = Digest::SHA256.digest(password)
  @cipher.iv = iv
  @cipher.update(encrypted) + @cipher.final
end

#encrypt(password, clear_data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dbchain_client/aes.rb', line 11

def encrypt(password, clear_data)
  iv  = generate_iv

  @cipher.encrypt
  @cipher.key = Digest::SHA256.digest(password)
  @cipher.iv  = iv

  encrypted = @cipher.update(clear_data) + @cipher.final
  encrypted_with_iv = prefix_iv(iv, encrypted) 
  Base64.strict_encode64(encrypted_with_iv)
end