Sha256: 3906df7e3ad5e5cf001cd941209f48120bd35888b786ebb9a2404eb66501f45b
Contents?: true
Size: 1.54 KB
Versions: 13
Compression:
Stored size: 1.54 KB
Contents
require 'crypt_keeper/log_subscriber/postgres_pgp' module CryptKeeper module Provider class PostgresPgpPublicKey include CryptKeeper::Helper::SQL 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? escape_and_execute_sql(["SELECT pgp_pub_decrypt(?, dearmor(?), ?)", value, @private_key, @key])['pgp_pub_decrypt'] else value end end # Public: Attempts to extract a PGP key id. If it's successful, it returns true # # Returns boolean def encrypted?(value) begin ActiveRecord::Base.transaction(requires_new: true) do escape_and_execute_sql(["SELECT pgp_key_id(?)", value.to_s])['pgp_key_id'].present? end rescue ActiveRecord::StatementInvalid false end end end end end
Version data entries
13 entries across 13 versions & 1 rubygems