Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Addin postgres_pgp

  • Loading branch information...
commit de5b6ca7b26a5e61d69806ec5e439d651e955421 1 parent 915257a
@jmazzi authored
View
2  .gitignore
@@ -15,3 +15,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
+spec/debug.log
+spec/database.yml
View
3  crypt_keeper_providers.gemspec
@@ -20,6 +20,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'guard-rspec', '~> 1.1.0'
gem.add_development_dependency 'rake', '~> 0.9.2.2'
gem.add_development_dependency 'jruby-openssl', '~> 0.7.7' if RUBY_PLATFORM == 'java'
-
+ gem.add_development_dependency 'activerecord', '>= 3.0'
+ gem.add_development_dependency 'pg'
end
View
1  lib/crypt_keeper_providers.rb
@@ -1,5 +1,6 @@
require 'crypt_keeper_providers/version'
require 'crypt_keeper_providers/aes'
+require 'crypt_keeper_providers/postgres_pgp'
module CryptKeeperProviders
end
View
36 lib/crypt_keeper_providers/postgres_pgp.rb
@@ -0,0 +1,36 @@
+module CryptKeeperProviders
+ 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
View
8 spec/default.database.yml
@@ -0,0 +1,8 @@
+test:
+ adapter: postgresql
+ encoding: utf8
+ reconnect: false
+ database: crypt_keeper_providers
+ pool: 5
+ username: justin
+ password:
View
25 spec/postgres_sql_spec.rb
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+module CryptKeeperProviders
+ describe PostgresPgp do
+ use_postgres
+ let(:cipher_text) { '\\xc30d0407030283b15f71b6a7d0296cd23501bd2c8fe3c7a56005ff4619527c4291509a78c77a6758cddd2a14acbde589fa10b3e0686865182d3beadaf237b9f928e7ba1810b8' }
+ let(:plain_text) { 'test' }
+ subject { PostgresPgp.new key: 'candy' }
+
+ its(:key) { should == 'candy' }
+
+ describe "#encrypt" do
+ it "should encrypt the string" do
+ subject.encrypt(plain_text).should_not == plain_text
+ subject.encrypt(plain_text).should_not be_empty
+ end
+ end
+
+ describe "#decrypt" do
+ it "should decrypt the string" do
+ subject.decrypt(cipher_text).should == plain_text
+ end
+ end
+ end
+end
View
22 spec/support/active_record.rb
@@ -0,0 +1,22 @@
+require 'active_record'
+require 'logger'
+require 'yaml'
+
+module CryptKeeperProviders
+ module ActiveRecord
+ # extend self
+
+ def use_postgres
+ before :all do
+ config = YAML.load_file SPEC_ROOT.join('database.yml')
+ ::ActiveRecord::Base.establish_connection(config['test'])
+ ::ActiveRecord::Base.logger = Logger.new SPEC_ROOT.join('debug.log').to_s
+ end
+ end
+ end
+end
+
+
+RSpec.configure do |config|
+ config.extend CryptKeeperProviders::ActiveRecord
+end
Please sign in to comment.
Something went wrong with that request. Please try again.