Sha256: 119a0b0213dc275baf17bfccf690690fd5345e2c51d405bfd4ae9152ee0ae985

Contents?: true

Size: 952 Bytes

Versions: 7

Compression:

Stored size: 952 Bytes

Contents

class PageletRails::Encryptor

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

  attr_reader :salt

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

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

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

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

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

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

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

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

  private

  def encryptor
    @encryptor ||= begin
      key = self.class.get_key secret, salt

      ActiveSupport::MessageEncryptor.new(key)
    end
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pagelet_rails-0.1.6 lib/pagelet_rails/encryptor.rb
pagelet_rails-0.1.5 lib/pagelet_rails/encryptor.rb
pagelet_rails-0.1.4 lib/pagelet_rails/encryptor.rb
pagelet_rails-0.1.3 lib/pagelet_rails/encryptor.rb
pagelet_rails-0.1.2 lib/pagelet_rails/encryptor.rb
pagelet_rails-0.1.1 lib/pagelet_rails/encryptor.rb
pagelet_rails-0.1.0 lib/pagelet_rails/encryptor.rb