Class: EncryptedField::PolicyWithIV

Inherits:
BasePolicy show all
Defined in:
lib/encrypted-field/policy_with_iv.rb

Overview

EncryptedField::PolicyWithIV all the logic required to encrypt/decrypt data using symmetric encryption.

Constant Summary collapse

DEFAULT_SEPARATOR =
'.'

Instance Attribute Summary

Attributes inherited from BasePolicy

#algorithm, #options

Instance Method Summary collapse

Methods inherited from BasePolicy

#initialize, #prefix_with_policy_name?

Constructor Details

This class inherits a constructor from EncryptedField::BasePolicy

Instance Method Details

#decrypt(encrypted_str) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/encrypted-field/policy_with_iv.rb', line 19

def decrypt(encrypted_str)
  iv, encrypted_str = encrypted_str.split(separator, 2)
  cipher = create_cipher.decrypt
  cipher.key = secret_key
  cipher.iv = decode_iv(iv)
  cipher.update(decode_payload(encrypted_str) << cipher.final)
end

#encrypt(str) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/encrypted-field/policy_with_iv.rb', line 11

def encrypt(str)
  cipher = create_cipher.encrypt
  cipher.key = secret_key
  iv = cipher.random_iv
  encrypted_str = cipher.update(str) << cipher.final
  encode_iv(iv) << separator << encode_payload(encrypted_str)
end

#separatorObject



27
28
29
# File 'lib/encrypted-field/policy_with_iv.rb', line 27

def separator
  options[:separator] || DEFAULT_SEPARATOR
end