Module: Sequel::Plugins::AttrEncrypted::SimpleCrypt

Extended by:
SimpleCrypt
Included in:
SimpleCrypt
Defined in:
lib/sequel/plugins/attr_encrypted/simple_crypt.rb

Constant Summary collapse

SEPARATOR =
"$"

Instance Method Summary collapse

Instance Method Details

#decrypt(string, key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/sequel/plugins/attr_encrypted/simple_crypt.rb', line 19

def decrypt(string, key)
  encrypted, iv, auth_tag = parse(string) if string.is_a?(String)
  return if [encrypted, iv, auth_tag].any?(&:nil?)

  decryptor = new_cipher(key, &:decrypt)
  decryptor.iv = iv
  decryptor.auth_tag = auth_tag

  decryptor.update(encrypted) + decryptor.final
end

#encrypt(string, key) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/sequel/plugins/attr_encrypted/simple_crypt.rb', line 9

def encrypt(string, key)
  return unless string.is_a?(String) && !string.empty?

  encryptor = new_cipher(key, &:encrypt)
  iv = encryptor.random_iv

  encrypted = encryptor.update(string) + encryptor.final
  dump(encrypted, iv, encryptor.auth_tag)
end