From b5bae00e45d3c6dc5ee48fe6c8e1d5d6431d56ef Mon Sep 17 00:00:00 2001 From: Eric Ti-Yu Chiang Date: Tue, 28 Oct 2014 12:22:57 -0400 Subject: [PATCH 1/5] add decrypt table (the reverse of encrypt table) --- lib/crypt_keeper/model.rb | 16 ++++++++++++++++ spec/model_spec.rb | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/crypt_keeper/model.rb b/lib/crypt_keeper/model.rb index 8edab61..e599cdc 100644 --- a/lib/crypt_keeper/model.rb +++ b/lib/crypt_keeper/model.rb @@ -98,6 +98,22 @@ def encrypt_table! end end + # Public: Decrypt a table (reverse of encrypt_table!) + def decrypt_table! + enc = encryptor_klass.new(crypt_keeper_options) + tmp_table = Class.new(ActiveRecord::Base).tap { |c| c.table_name = self.table_name } + + transaction do + tmp_table.find_each do |r| + crypt_keeper_fields.each do |field| + r.send("#{field}=", enc.decrypt(r[field])) if r[field].present? + end + + r.save! + end + end + end + private # Private: The encryptor class diff --git a/spec/model_spec.rb b/spec/model_spec.rb index eb618b7..8dc886e 100644 --- a/spec/model_spec.rb +++ b/spec/model_spec.rb @@ -124,6 +124,21 @@ module CryptKeeper end end + context "Table Decryption (Reverse of Initial Table Encryption)" do + subject { create_encrypted_model :storage, key: 'tool', salt: 'salt', encryptor: :aes_new } + let!(:storage_entries) { 5.times.map { |i| "testing#{i}" } } + + before do + subject.delete_all + storage_entries.each { |entry| subject.create! storage: entry} + end + + it "decrypts the table" do + subject.decrypt_table! + expect( create_model.first(5).map(&:storage) ).to eq( storage_entries ) + end + end + context "Missing Attributes" do subject { create_encrypted_model :storage, key: 'tool', salt: 'salt', encryptor: :aes_new, encoding: 'utf-8' } From 43e5e53ca1eac7cd7cd94efdc71e3d9f6aefdc73 Mon Sep 17 00:00:00 2001 From: Dan Kohn Date: Tue, 6 Jan 2015 21:06:45 -0500 Subject: [PATCH 2/5] Added Rails 4.2 support --- crypt_keeper.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crypt_keeper.gemspec b/crypt_keeper.gemspec index 17e2f54..ace666d 100644 --- a/crypt_keeper.gemspec +++ b/crypt_keeper.gemspec @@ -16,8 +16,8 @@ Gem::Specification.new do |gem| gem.require_paths = ["lib"] gem.version = CryptKeeper::VERSION - gem.add_runtime_dependency 'activerecord', '>= 3.1', '< 4.2' - gem.add_runtime_dependency 'activesupport', '>= 3.1', '< 4.2' + gem.add_runtime_dependency 'activerecord', '>= 3.1', '<= 4.2' + gem.add_runtime_dependency 'activesupport', '>= 3.1', '<= 4.2' gem.add_runtime_dependency 'aes', '~> 0.5.0' gem.add_runtime_dependency 'armor', '~> 0.0.2' From 0cbf927a2a58d10e7b788c8ef72af608198dcf8e Mon Sep 17 00:00:00 2001 From: Dan Kohn Date: Tue, 6 Jan 2015 21:08:00 -0500 Subject: [PATCH 3/5] Rails 4.2 support --- crypt_keeper.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crypt_keeper.gemspec b/crypt_keeper.gemspec index 17e2f54..ace666d 100644 --- a/crypt_keeper.gemspec +++ b/crypt_keeper.gemspec @@ -16,8 +16,8 @@ Gem::Specification.new do |gem| gem.require_paths = ["lib"] gem.version = CryptKeeper::VERSION - gem.add_runtime_dependency 'activerecord', '>= 3.1', '< 4.2' - gem.add_runtime_dependency 'activesupport', '>= 3.1', '< 4.2' + gem.add_runtime_dependency 'activerecord', '>= 3.1', '<= 4.2' + gem.add_runtime_dependency 'activesupport', '>= 3.1', '<= 4.2' gem.add_runtime_dependency 'aes', '~> 0.5.0' gem.add_runtime_dependency 'armor', '~> 0.0.2' From 4132b1f2d3821dfeed912962bd68ded2d141d648 Mon Sep 17 00:00:00 2001 From: Eric Ti-Yu Chiang Date: Tue, 28 Oct 2014 12:22:57 -0400 Subject: [PATCH 4/5] add decrypt table (the reverse of encrypt table) --- lib/crypt_keeper/model.rb | 16 ++++++++++++++++ spec/model_spec.rb | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/crypt_keeper/model.rb b/lib/crypt_keeper/model.rb index 8edab61..e599cdc 100644 --- a/lib/crypt_keeper/model.rb +++ b/lib/crypt_keeper/model.rb @@ -98,6 +98,22 @@ def encrypt_table! end end + # Public: Decrypt a table (reverse of encrypt_table!) + def decrypt_table! + enc = encryptor_klass.new(crypt_keeper_options) + tmp_table = Class.new(ActiveRecord::Base).tap { |c| c.table_name = self.table_name } + + transaction do + tmp_table.find_each do |r| + crypt_keeper_fields.each do |field| + r.send("#{field}=", enc.decrypt(r[field])) if r[field].present? + end + + r.save! + end + end + end + private # Private: The encryptor class diff --git a/spec/model_spec.rb b/spec/model_spec.rb index 4a82c14..4f26575 100644 --- a/spec/model_spec.rb +++ b/spec/model_spec.rb @@ -124,6 +124,21 @@ module CryptKeeper end end + context "Table Decryption (Reverse of Initial Table Encryption)" do + subject { create_encrypted_model :storage, key: 'tool', salt: 'salt', encryptor: :aes_new } + let!(:storage_entries) { 5.times.map { |i| "testing#{i}" } } + + before do + subject.delete_all + storage_entries.each { |entry| subject.create! storage: entry} + end + + it "decrypts the table" do + subject.decrypt_table! + expect( create_model.first(5).map(&:storage) ).to eq( storage_entries ) + end + end + context "Missing Attributes" do subject { create_encrypted_model :storage, key: 'tool', salt: 'salt', encryptor: :aes_new, encoding: 'utf-8' } From 7ff84bead37e35191d0ac9a74a8d0ba47252a29c Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Thu, 26 Feb 2015 15:36:38 -0500 Subject: [PATCH 5/5] update crypt_keeper version in multiple gemfile.lock used by travis ci --- gemfiles/activerecord_3_1.gemfile.lock | 2 +- gemfiles/activerecord_3_2.gemfile.lock | 2 +- gemfiles/activerecord_4_0.gemfile.lock | 2 +- gemfiles/activerecord_4_1.gemfile.lock | 2 +- gemfiles/activerecord_4_2.gemfile.lock | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gemfiles/activerecord_3_1.gemfile.lock b/gemfiles/activerecord_3_1.gemfile.lock index fe4b071..76867ee 100644 --- a/gemfiles/activerecord_3_1.gemfile.lock +++ b/gemfiles/activerecord_3_1.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../ specs: - crypt_keeper (0.18.4) + crypt_keeper (0.19.0) activerecord (>= 3.1, < 4.3) activesupport (>= 3.1, < 4.3) aes (~> 0.5.0) diff --git a/gemfiles/activerecord_3_2.gemfile.lock b/gemfiles/activerecord_3_2.gemfile.lock index 62fc805..b6dfba2 100644 --- a/gemfiles/activerecord_3_2.gemfile.lock +++ b/gemfiles/activerecord_3_2.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../ specs: - crypt_keeper (0.18.4) + crypt_keeper (0.19.0) activerecord (>= 3.1, < 4.3) activesupport (>= 3.1, < 4.3) aes (~> 0.5.0) diff --git a/gemfiles/activerecord_4_0.gemfile.lock b/gemfiles/activerecord_4_0.gemfile.lock index a9367ef..4dfaffc 100644 --- a/gemfiles/activerecord_4_0.gemfile.lock +++ b/gemfiles/activerecord_4_0.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../ specs: - crypt_keeper (0.18.4) + crypt_keeper (0.19.0) activerecord (>= 3.1, < 4.3) activesupport (>= 3.1, < 4.3) aes (~> 0.5.0) diff --git a/gemfiles/activerecord_4_1.gemfile.lock b/gemfiles/activerecord_4_1.gemfile.lock index bef8036..f70fbbc 100644 --- a/gemfiles/activerecord_4_1.gemfile.lock +++ b/gemfiles/activerecord_4_1.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../ specs: - crypt_keeper (0.18.4) + crypt_keeper (0.19.0) activerecord (>= 3.1, < 4.3) activesupport (>= 3.1, < 4.3) aes (~> 0.5.0) diff --git a/gemfiles/activerecord_4_2.gemfile.lock b/gemfiles/activerecord_4_2.gemfile.lock index a51bf7e..7b7d168 100644 --- a/gemfiles/activerecord_4_2.gemfile.lock +++ b/gemfiles/activerecord_4_2.gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../ specs: - crypt_keeper (0.18.4) + crypt_keeper (0.19.0) activerecord (>= 3.1, < 4.3) activesupport (>= 3.1, < 4.3) aes (~> 0.5.0)