Class: Decidim::Initiatives::DataEncryptor

Inherits:
Object
  • Object
show all
Defined in:
decidim-initiatives/app/services/decidim/initiatives/data_encryptor.rb

Overview

Service to encrypt and decrypt metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ DataEncryptor

Returns a new instance of DataEncryptor.


9
10
11
12
13
14
15
# File 'decidim-initiatives/app/services/decidim/initiatives/data_encryptor.rb', line 9

def initialize(args = {})
  @secret = args.fetch(:secret) || "default"
  @key = ActiveSupport::KeyGenerator.new(secret).generate_key(
    Rails.application.secrets.secret_key_base, ActiveSupport::MessageEncryptor.key_len
  )
  @encryptor = ActiveSupport::MessageEncryptor.new(@key)
end

Instance Attribute Details

#secretObject (readonly)

Returns the value of attribute secret


7
8
9
# File 'decidim-initiatives/app/services/decidim/initiatives/data_encryptor.rb', line 7

def secret
  @secret
end

Instance Method Details

#decrypt(encrypted_data) ⇒ Object


21
22
23
# File 'decidim-initiatives/app/services/decidim/initiatives/data_encryptor.rb', line 21

def decrypt(encrypted_data)
  @encryptor.decrypt_and_verify(encrypted_data)
end

#encrypt(data) ⇒ Object


17
18
19
# File 'decidim-initiatives/app/services/decidim/initiatives/data_encryptor.rb', line 17

def encrypt(data)
  @encryptor.encrypt_and_sign(data)
end