Class: PageletRails::Encryptor
- Inherits:
-
Object
- Object
- PageletRails::Encryptor
- Defined in:
- lib/pagelet_rails/encryptor.rb
Constant Summary collapse
- DEFAULT_SALT =
'!@#Q156^tdSXggT0&*789++8&?_|T%\/++==RqE'
Instance Attribute Summary collapse
-
#salt ⇒ Object
readonly
Returns the value of attribute salt.
Class Method Summary collapse
- .decode(encrypted_data, opts = {}) ⇒ Object
- .encode(data, opts = {}) ⇒ Object
- .get_key(secret, salt) ⇒ Object
Instance Method Summary collapse
- #decode(encrypted_data) ⇒ Object
- #encode(data) ⇒ Object
-
#initialize(opts = {}) ⇒ Encryptor
constructor
A new instance of Encryptor.
- #secret ⇒ Object
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
#salt ⇒ Object (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 |
#secret ⇒ Object
27 28 29 |
# File 'lib/pagelet_rails/encryptor.rb', line 27 def secret @secret || Rails.application.secrets[:secret_key_base] end |