require 'crypt_keeper/log_subscriber/postgres_pgp' module CryptKeeper module Provider 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 end