Sha256: 11278cdb2a64b64ba053505416d18043784b28403f0551c63a11d0a0c7262d0b

Contents?: true

Size: 974 Bytes

Versions: 5

Compression:

Stored size: 974 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
      key = key[0..31]
      ActiveSupport::MessageEncryptor.new(key)
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pagelet_rails-0.2.2 lib/pagelet_rails/encryptor.rb
pagelet_rails-0.2.1 lib/pagelet_rails/encryptor.rb
pagelet_rails-0.2.0 lib/pagelet_rails/encryptor.rb
pagelet_rails-0.1.8 lib/pagelet_rails/encryptor.rb
pagelet_rails-0.1.7 lib/pagelet_rails/encryptor.rb