Class: ActiveEncryption::Encryptor

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

Overview

The ActiveEncryption::Encryptor is an abstraction class around ActiveSupport::MessageEncryptor

Usage:

encryptor = ActiveEncryption::Encryptor.new(encryption_setting) encrypted_data = encryptor.encrypt(data) data = encryptor.decrypt(encrypted_data)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encryption_setting, service = nil) ⇒ Encryptor

Returns a new instance of Encryptor.



18
19
20
21
# File 'lib/active_encryption/encryptor.rb', line 18

def initialize(encryption_setting, service = nil)
  @encryption_setting = encryption_setting
  @service = service
end

Instance Attribute Details

#encryption_settingObject (readonly)

Returns the value of attribute encryption_setting.



16
17
18
# File 'lib/active_encryption/encryptor.rb', line 16

def encryption_setting
  @encryption_setting
end

Instance Method Details

#decrypt(data, purpose: nil) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/active_encryption/encryptor.rb', line 35

def decrypt(data, purpose: nil)
  return nil unless data

  service.decrypt_and_verify(
    data,
    purpose: purpose
  )
end

#encrypt(data, expires_at: nil, expires_in: nil, purpose: nil) ⇒ Object

:reek:LongParameterList is required to map to encrypt_and_sign



24
25
26
27
28
29
30
31
32
33
# File 'lib/active_encryption/encryptor.rb', line 24

def encrypt(data, expires_at: nil, expires_in: nil, purpose: nil)
  return nil unless data

  service.encrypt_and_sign(
    data,
    expires_at: expires_at,
    expires_in: expires_in,
    purpose: purpose
  )
end