Class: PageletRails::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/pagelet_rails/encryptor.rb

Constant Summary collapse

DEFAULT_SALT =
'!@#Q156^tdSXggT0&*789++8&?_|T%\/++==RqE'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Encryptor



22
23
24
25
# File 'lib/pagelet_rails/encryptor.rb', line 22

def initialize(opts = {})
  @salt = opts.fetch :salt, DEFAULT_SALT
  @secret = opts[:secret]
end

Instance Attribute Details

#saltObject (readonly)

Returns the value of attribute salt.



5
6
7
# File 'lib/pagelet_rails/encryptor.rb', line 5

def salt
  @salt
end

Class Method Details

.decode(encrypted_data, opts = {}) ⇒ Object



11
12
13
# File 'lib/pagelet_rails/encryptor.rb', line 11

def self.decode(encrypted_data, opts = {})
  self.new(opts).decode encrypted_data
end

.encode(data, opts = {}) ⇒ Object



7
8
9
# File 'lib/pagelet_rails/encryptor.rb', line 7

def self.encode(data, opts = {})
  self.new(opts).encode data
end

.get_key(secret, salt) ⇒ Object



15
16
17
18
19
20
# File 'lib/pagelet_rails/encryptor.rb', line 15

def self.get_key secret, salt
  @get_key_cache ||= {}
  key = [secret, salt]

  @get_key_cache[key] ||= ActiveSupport::KeyGenerator.new(secret).generate_key(salt)
end

Instance Method Details

#decode(encrypted_data) ⇒ Object



35
36
37
# File 'lib/pagelet_rails/encryptor.rb', line 35

def decode(encrypted_data)
  encryptor.decrypt_and_verify(encrypted_data)
end

#encode(data) ⇒ Object



31
32
33
# File 'lib/pagelet_rails/encryptor.rb', line 31

def encode(data)
  encryptor.encrypt_and_sign(data)
end

#secretObject



27
28
29
# File 'lib/pagelet_rails/encryptor.rb', line 27

def secret
  @secret || Rails.application.secrets[:secret_key_base]
end