Sha256: 333100521a4f2a43ed7ae8a43beed0fd10cb6acbc4b5a47fd8d9355f42838175

Contents?: true

Size: 937 Bytes

Versions: 6

Compression:

Stored size: 937 Bytes

Contents

require 'crypt_keeper/log_subscriber/mysql_aes'

module CryptKeeper
  module Provider
    class MysqlAes
      include CryptKeeper::Helper::SQL

      attr_accessor :key

      # Public: Initializes the encryptor
      #
      #  options - A hash, :key is required
      def initialize(options = {})
        ActiveSupport.run_load_hooks(:crypt_keeper_mysql_aes_log, self)

        @key = options.fetch(:key) do
          raise ArgumentError, "Missing :key"
        end
      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
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
crypt_keeper-0.13.1 lib/crypt_keeper/provider/mysql_aes.rb
crypt_keeper-0.13.0 lib/crypt_keeper/provider/mysql_aes.rb
crypt_keeper-0.12.0 lib/crypt_keeper/provider/mysql_aes.rb
crypt_keeper-0.10.0.pre lib/crypt_keeper/provider/mysql_aes.rb
crypt_keeper-0.9.0.pre lib/crypt_keeper/provider/mysql_aes.rb
crypt_keeper-0.8.0 lib/crypt_keeper/provider/mysql_aes.rb