Class: Relish::EncryptionHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/relish/encryption_helper.rb

Constant Summary collapse

LEGACY_MATCHER =
/.+?\|.+?\|.+?/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(static_secret, secrets) ⇒ EncryptionHelper

Returns a new instance of EncryptionHelper.



13
14
15
16
# File 'lib/relish/encryption_helper.rb', line 13

def initialize(static_secret, secrets)
  @static_secret = static_secret
  @secrets = secrets
end

Instance Method Details

#decrypt(key = 'env', token) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/relish/encryption_helper.rb', line 28

def decrypt(key = 'env', token)
  plain = nil
  hmac_secrets.each do |secret|
    plain = decrypt_with_secret(secret, token, key)
    break if plain
  end
  raise RelishDecryptionFailed unless plain
  plain
end

#encrypt(_key = 'env', value) ⇒ Object



18
19
20
# File 'lib/relish/encryption_helper.rb', line 18

def encrypt(_key = 'env', value)
  current_encrypt(value)
end

#inspectObject Also known as: to_s



38
39
40
# File 'lib/relish/encryption_helper.rb', line 38

def inspect
  "#<Relish::EncryptionHelper>"
end

#legacy_encrypt(key, value) ⇒ Object



22
23
24
25
26
# File 'lib/relish/encryption_helper.rb', line 22

def legacy_encrypt(key, value)
  Fernet::Legacy.generate(hmac_secrets.first) do |gen|
    gen.data = { key => value }
  end
end