Sha256: a865b9bd4076f45724e53c8ade5a57efee847c19201de845e49b5fca683a0479

Contents?: true

Size: 929 Bytes

Versions: 35

Compression:

Stored size: 929 Bytes

Contents

# frozen_string_literal: true

module Decidim
  class AttributeEncryptor
    def self.encrypt(string)
      cryptor.encrypt_and_sign(string) if string.present?
    end

    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

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

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
decidim-core-0.26.10 lib/decidim/attribute_encryptor.rb
decidim-core-0.26.9 lib/decidim/attribute_encryptor.rb
decidim-core-0.28.0 lib/decidim/attribute_encryptor.rb
decidim-core-0.27.5 lib/decidim/attribute_encryptor.rb
decidim-core-0.28.0.rc5 lib/decidim/attribute_encryptor.rb
decidim-core-0.28.0.rc4 lib/decidim/attribute_encryptor.rb
decidim-core-0.26.8 lib/decidim/attribute_encryptor.rb
decidim-core-0.27.4 lib/decidim/attribute_encryptor.rb
decidim-core-0.27.3 lib/decidim/attribute_encryptor.rb
decidim-core-0.26.7 lib/decidim/attribute_encryptor.rb
decidim-core-0.26.5 lib/decidim/attribute_encryptor.rb
decidim-core-0.27.2 lib/decidim/attribute_encryptor.rb
decidim-core-0.27.1 lib/decidim/attribute_encryptor.rb
decidim-core-0.26.4 lib/decidim/attribute_encryptor.rb
decidim-core-0.27.0 lib/decidim/attribute_encryptor.rb
decidim-core-0.26.3 lib/decidim/attribute_encryptor.rb
decidim-core-0.27.0.rc2 lib/decidim/attribute_encryptor.rb
decidim-core-0.27.0.rc1 lib/decidim/attribute_encryptor.rb
decidim-core-0.26.2 lib/decidim/attribute_encryptor.rb
decidim-core-0.26.1 lib/decidim/attribute_encryptor.rb