Class: RomEncryptedAttribute::Encryptor

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

Instance Method Summary collapse

Constructor Details

#initialize(derivator:) ⇒ Encryptor

Returns a new instance of Encryptor.



10
11
12
# File 'lib/rom_encrypted_attribute/encryptor.rb', line 10

def initialize(derivator:)
  @derivator = derivator
end

Instance Method Details

#encrypt(message) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rom_encrypted_attribute/encryptor.rb', line 14

def encrypt(message)
  cipher = OpenSSL::Cipher.new("aes-256-gcm")
  key = @derivator.derive(cipher.key_len)
  iv = cipher.random_iv

  cipher.encrypt
  cipher.key = key
  cipher.iv = iv
  encrypted = cipher.update(message) + cipher.final
  Payload.new(message: encrypted, initialization_vector: iv, auth_tag: cipher.auth_tag).encode
end