Class: RomEncryptedAttribute::Decryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/rom_encrypted_attribute/decryptor.rb

Instance Method Summary collapse

Constructor Details

#initialize(derivator:) ⇒ Decryptor

Returns a new instance of Decryptor.



9
10
11
# File 'lib/rom_encrypted_attribute/decryptor.rb', line 9

def initialize(derivator:)
  @derivator = derivator
end

Instance Method Details

#decrypt(message) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rom_encrypted_attribute/decryptor.rb', line 13

def decrypt(message)
  payload = RomEncryptedAttribute::Payload.decode(message)

  cipher = OpenSSL::Cipher.new("aes-256-gcm")
  key = @derivator.derive(cipher.key_len)

  cipher.decrypt
  cipher.padding = 0
  cipher.key = key
  cipher.iv = payload.initialization_vector
  cipher.auth_tag = payload.auth_tag
  cipher.auth_data = ""
  cipher.update(payload.message) + cipher.final
rescue JSON::ParserError
  # we need to unconditionally support of reading unencrypted data due to a bug in rom-sql
  # https://github.com/rom-rb/rom-sql/issues/423
  message
end