Sha256: ebf00980a8c4e9537d29247f28c5463bd9f46f668e1875b235e53f6d17646161

Contents?: true

Size: 1001 Bytes

Versions: 6

Compression:

Stored size: 1001 Bytes

Contents

require 'crypt_keeper_providers/postgres_pgp/log_subscriber'

module CryptKeeperProviders
  class PostgresPgp
    attr_accessor :key

    # Public: Initializes the encryptor
    #
    #  options - A hash, :key is required
    def initialize(options = {})
      @key = options.fetch(:key) do
        raise ArgumentError, "Missing :key"
      end
    end

    # Public: Encrypts a string
    #
    # Returns an encrypted string
    def encrypt(value)
      escape_and_execute_sql(["SELECT pgp_sym_encrypt(?, ?)", value, key])['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

    private

    # Private: Sanitize an sql query and then execute it
    def escape_and_execute_sql(query)
      query = ::ActiveRecord::Base.send :sanitize_sql_array, query
      ::ActiveRecord::Base.connection.execute(query).first
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
crypt_keeper_providers-0.5.2 lib/crypt_keeper_providers/postgres_pgp.rb
crypt_keeper_providers-0.5.1 lib/crypt_keeper_providers/postgres_pgp.rb
crypt_keeper_providers-0.5.0 lib/crypt_keeper_providers/postgres_pgp.rb
crypt_keeper_providers-0.4.0 lib/crypt_keeper_providers/postgres_pgp.rb
crypt_keeper_providers-0.3.0 lib/crypt_keeper_providers/postgres_pgp.rb
crypt_keeper_providers-0.2.0 lib/crypt_keeper_providers/postgres_pgp.rb