Sha256: ed35ebe25727c3d4a176deff6c88382bcafd09a80d26d2d120c508b53994fa8c

Contents?: true

Size: 1.17 KB

Versions: 13

Compression:

Stored size: 1.17 KB

Contents

require 'crypt_keeper/log_subscriber/postgres_pgp'

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

      attr_accessor :key
      attr_accessor :pgcrypto_options

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

        @key = options.fetch(:key) do
          raise ArgumentError, "Missing :key"
        end

        @pgcrypto_options = options.fetch(:pgcrypto_options, '')
      end

      # Public: Encrypts a string
      #
      # Returns an encrypted string
      def encrypt(value)
        escape_and_execute_sql(["SELECT pgp_sym_encrypt(?, ?, ?)", value.to_s, key, pgcrypto_options])['pgp_sym_encrypt']
      end

      # Public: Decrypts a string
      #
      # Returns a plaintext string
      def decrypt(value)
        escape_and_execute_sql(["SELECT pgp_sym_decrypt(?, ?)", value, key])['pgp_sym_decrypt']
      end

      def search(records, field, criteria)
        records.where("(pgp_sym_decrypt(cast(#{field} AS bytea), ?) = ?)", key, criteria)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
crypt_keeper-0.22.0 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.21.0 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.20.0 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.19.0 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.18.4 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.18.3 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.18.2 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.18.1 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.18.0 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.17.0 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.16.1 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.16.0 lib/crypt_keeper/provider/postgres_pgp.rb
crypt_keeper-0.16.0.pre lib/crypt_keeper/provider/postgres_pgp.rb