Class: Decidim::AttributeEncryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/attribute_encryptor.rb

Class Method Summary collapse

Class Method Details

.cryptorObject



21
22
23
24
25
26
27
28
# File 'lib/decidim/attribute_encryptor.rb', line 21

def self.cryptor
  @cryptor ||= begin
    key = ActiveSupport::KeyGenerator.new("attribute").generate_key(
      Rails.application.secrets.secret_key_base, ActiveSupport::MessageEncryptor.key_len
    )
    ActiveSupport::MessageEncryptor.new(key)
  end
end

.decrypt(string_encrypted) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/decidim/attribute_encryptor.rb', line 9

def self.decrypt(string_encrypted)
  return if string_encrypted.blank?

  # `ActiveSupport::MessageEncryptor` expects all values passed to the
  # `#decrypt_and_verify` method to be instances of String as the message
  # verifier calls `#split` on the value objects: https://git.io/JqfOO.
  # If something else is passed, just return the value as is.
  return string_encrypted unless string_encrypted.is_a?(String)

  cryptor.decrypt_and_verify(string_encrypted)
end

.encrypt(string) ⇒ Object



5
6
7
# File 'lib/decidim/attribute_encryptor.rb', line 5

def self.encrypt(string)
  cryptor.encrypt_and_sign(string) if string.present?
end