Class: CryptKeeper::Provider::PostgresPgpPublicKey

Inherits:
PostgresBase show all
Defined in:
lib/crypt_keeper/provider/postgres_pgp_public_key.rb

Constant Summary

Constants inherited from PostgresBase

CryptKeeper::Provider::PostgresBase::INVALID_DATA_ERROR

Constants included from LogSubscriber::PostgresPgp

LogSubscriber::PostgresPgp::FILTER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PostgresBase

#encrypted?

Methods included from LogSubscriber::PostgresPgp

#sql

Methods inherited from Base

#dump, #load

Constructor Details

#initialize(options = {}) ⇒ PostgresPgpPublicKey

Returns a new instance of PostgresPgpPublicKey.


6
7
8
9
10
11
12
13
14
15
# File 'lib/crypt_keeper/provider/postgres_pgp_public_key.rb', line 6

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

Instance Attribute Details

#keyObject

Returns the value of attribute key


4
5
6
# File 'lib/crypt_keeper/provider/postgres_pgp_public_key.rb', line 4

def key
  @key
end

Instance Method Details

#decrypt(value) ⇒ Object

Public: Decrypts a string

Returns a plaintext string


31
32
33
34
35
36
37
38
# File 'lib/crypt_keeper/provider/postgres_pgp_public_key.rb', line 31

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

#encrypt(value) ⇒ Object

Public: Encrypts a string

Returns an encrypted string


20
21
22
23
24
25
26
# File 'lib/crypt_keeper/provider/postgres_pgp_public_key.rb', line 20

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