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) 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' }