Class: Chamber::EncryptionMethods::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
lib/chamber/encryption_methods/public_key.rb

Class Method Summary collapse

Class Method Details

.decrypt(_settings_key, value, decryption_key) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chamber/encryption_methods/public_key.rb', line 17

def self.decrypt(_settings_key, value, decryption_key)
  return value if decryption_key.nil?

  decoded_string    = ::Base64.strict_decode64(value)
  unencrypted_value = decryption_key.private_decrypt(decoded_string)

  ::YAML.safe_load(unencrypted_value,
                   aliases:           true,
                   permitted_classes: [
                                        ::Date,
                                        ::Time,
                                        ::Regexp,
                                      ])
rescue ::Psych::DisallowedClass => error
  raise ::Chamber::Errors::DisallowedClass, <<~HEREDOC
    #{error.message}

    You attempted to load a class instance via your Chamber settings that is not allowed.

    See https://github.com/thekompanee/chamber/wiki/Upgrading-To-Chamber-3.0#limiting-complex-classes for full details.
  HEREDOC
rescue ::TypeError
  unencrypted_value
end

.encrypt(_settings_key, value, encryption_key) ⇒ Object



10
11
12
13
14
15
# File 'lib/chamber/encryption_methods/public_key.rb', line 10

def self.encrypt(_settings_key, value, encryption_key)
  value            = YAML.dump(value)
  encrypted_string = encryption_key.public_encrypt(value)

  Base64.strict_encode64(encrypted_string)
end