Class: CryptKeeperProviders::PostgresPgp

Inherits:
Object
  • Object
show all
Defined in:
lib/crypt_keeper_providers/postgres_pgp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PostgresPgp

Public: Initializes the encryptor

options - A hash, :key is required


10
11
12
13
14
# File 'lib/crypt_keeper_providers/postgres_pgp.rb', line 10

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

Instance Attribute Details

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/crypt_keeper_providers/postgres_pgp.rb', line 5

def key
  @key
end

Instance Method Details

#decrypt(value) ⇒ Object

Public: Decrypts a string

Returns a plaintext string



26
27
28
# File 'lib/crypt_keeper_providers/postgres_pgp.rb', line 26

def decrypt(value)
  escape_and_execute_sql(["SELECT pgp_sym_decrypt(?, ?)", value, key])['pgp_sym_decrypt']
end

#encrypt(value) ⇒ Object

Public: Encrypts a string

Returns an encrypted string



19
20
21
# File 'lib/crypt_keeper_providers/postgres_pgp.rb', line 19

def encrypt(value)
  escape_and_execute_sql(["SELECT pgp_sym_encrypt(?, ?)", value, key])['pgp_sym_encrypt']
end