Sha256: 6fc2e1364452b373d74bcc7c0854aacd8bf45cfd0afb89da38dbc55bb9161c4f

Contents?: true

Size: 1.36 KB

Versions: 15

Compression:

Stored size: 1.36 KB

Contents

require 'openssl'
require 'base64'
require 'hashie/mash'

module    Chamber
module    Filters
class     EncryptionFilter
  SECURE_KEY_TOKEN      = %r{\A_secure_}
  BASE64_STRING_PATTERN = %r{\A[A-Za-z0-9\+\/]{342}==\z}

  def initialize(options = {})
    self.encryption_key = options.fetch(:encryption_key, nil)
    self.data           = options.fetch(:data).dup
  end

  def self.execute(options = {})
    self.new(options).send(:execute)
  end

  protected

  attr_accessor :data
  attr_reader   :encryption_key

  def execute(raw_data = data)
    settings = Hashie::Mash.new

    raw_data.each_pair do |key, value|
      if value.respond_to? :each_pair
        value = execute(value)
      elsif value.respond_to? :match
        if key.match(SECURE_KEY_TOKEN) && !value.match(BASE64_STRING_PATTERN)
          encrypted_string = encryption_key.public_encrypt(value)
          value            = Base64.strict_encode64(encrypted_string)
        end
      end

      settings[key] = value
    end

    settings
  end

  def encryption_key=(keyish)
    return @encryption_key = nil if keyish.nil?

    key_content     = if ::File.readable?(::File.expand_path(keyish))
                        ::File.read(::File.expand_path(keyish))
                      else
                        keyish
                      end

    @encryption_key = OpenSSL::PKey::RSA.new(key_content)
  end
end
end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
chamber-2.4.0 lib/chamber/filters/encryption_filter.rb
chamber-2.3.2 lib/chamber/filters/encryption_filter.rb
chamber-2.3.1 lib/chamber/filters/encryption_filter.rb
chamber-2.3.0 lib/chamber/filters/encryption_filter.rb
chamber-2.2.1 lib/chamber/filters/encryption_filter.rb
chamber-2.2.0 lib/chamber/filters/encryption_filter.rb
chamber-2.1.9 lib/chamber/filters/encryption_filter.rb
chamber-2.1.8 lib/chamber/filters/encryption_filter.rb
chamber-2.1.7 lib/chamber/filters/encryption_filter.rb
chamber-2.1.6 lib/chamber/filters/encryption_filter.rb
chamber-2.1.3 lib/chamber/filters/encryption_filter.rb
chamber-2.1.2 lib/chamber/filters/encryption_filter.rb
chamber-2.1.1 lib/chamber/filters/encryption_filter.rb
chamber-2.1.0 lib/chamber/filters/encryption_filter.rb
chamber-2.0.0 lib/chamber/filters/encryption_filter.rb