Sha256: 6676bcb6d354df81643f72a093b35524e60920a7eccebc221e8aea714457c0f6

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

require 'crypt_keeper/log_subscriber/mysql_aes'

module CryptKeeper
  module Provider
    class MysqlAesNew < Base
      include CryptKeeper::Helper::SQL
      include CryptKeeper::Helper::DigestPassphrase

      attr_accessor :key

      # Public: Initializes the encryptor
      #
      #  options - A hash, :key and :salt are required
      def initialize(options = {})
        ::ActiveSupport.run_load_hooks(:crypt_keeper_mysql_aes_log, self)
        @key = digest_passphrase(options[:key], options[:salt])
      end

      # Public: Encrypts a string
      #
      # Returns an encrypted string
      def encrypt(value)
        Base64.encode64 escape_and_execute_sql(
          ["SELECT AES_ENCRYPT(?, ?)", value, key]).first
      end

      # Public: Decrypts a string
      #
      # Returns a plaintext string
      def decrypt(value)
        escape_and_execute_sql(
          ["SELECT AES_DECRYPT(?, ?)", Base64.decode64(value), key]).first
      end

      # Public: Searches the table
      #
      # Returns an Enumerable
      def search(records, field, criteria)
        records.where("#{field} = ?", encrypt(criteria))
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
crypt_keeper-2.3.0 lib/crypt_keeper/provider/mysql_aes_new.rb
crypt_keeper-2.2.0 lib/crypt_keeper/provider/mysql_aes_new.rb
crypt_keeper-2.1.0 lib/crypt_keeper/provider/mysql_aes_new.rb
crypt_keeper-2.0.1 lib/crypt_keeper/provider/mysql_aes_new.rb
crypt_keeper-2.0.0.rc2 lib/crypt_keeper/provider/mysql_aes_new.rb
crypt_keeper-2.0.0.rc1 lib/crypt_keeper/provider/mysql_aes_new.rb