Sha256: 177fa36aa31bd70abc51ff98e281aa5a81adc442c3b8cb395f8715025037096d

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

module CryptKeeper
  module Provider
    class PostgresPgpPublicKey < PostgresBase
      attr_accessor :key

      def initialize(options = {})
        ::ActiveSupport.run_load_hooks(:crypt_keeper_postgres_pgp_log, self)

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

        @public_key  = options.fetch(:public_key)
        @private_key = options[:private_key]
      end

      # Public: Encrypts a string
      #
      # Returns an encrypted string
      def encrypt(value)
        if !@private_key.present? && encrypted?(value)
          value
        else
          escape_and_execute_sql(["SELECT pgp_pub_encrypt(?, dearmor(?))", value.to_s, @public_key])['pgp_pub_encrypt']
        end
      end

      # Public: Decrypts a string
      #
      # Returns a plaintext string
      def decrypt(value)
        if @private_key.present? && encrypted?(value)
          escape_and_execute_sql(["SELECT pgp_pub_decrypt(?, dearmor(?), ?)",
            value, @private_key, @key])['pgp_pub_decrypt']
        else
          value
        end
      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/postgres_pgp_public_key.rb
crypt_keeper-2.2.0 lib/crypt_keeper/provider/postgres_pgp_public_key.rb
crypt_keeper-2.1.0 lib/crypt_keeper/provider/postgres_pgp_public_key.rb
crypt_keeper-2.0.1 lib/crypt_keeper/provider/postgres_pgp_public_key.rb
crypt_keeper-2.0.0.rc2 lib/crypt_keeper/provider/postgres_pgp_public_key.rb
crypt_keeper-2.0.0.rc1 lib/crypt_keeper/provider/postgres_pgp_public_key.rb