Class: ROM::EncryptedAttribute::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.



10
11
12
# File 'lib/rom/encrypted_attribute/decryptor.rb', line 10

def initialize(derivator:)
  @derivator = derivator
end

Instance Method Details

#decrypt(message) ⇒ Object



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

def decrypt(message)
  payload = ROM::EncryptedAttribute::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