Skip to content

Commit

Permalink
Merge pull request #87 from spreemo/add-decrypt-table
Browse files Browse the repository at this point in the history
add decrypt table (the reverse of encrypt table)
  • Loading branch information
jmazzi committed Feb 27, 2015
2 parents 8f7f0cf + 7ff84be commit 1891041
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gemfiles/activerecord_3_1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/activerecord_3_2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/activerecord_4_0.gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/activerecord_4_1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/activerecord_4_2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 16 additions & 0 deletions lib/crypt_keeper/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }

Expand Down

0 comments on commit 1891041

Please sign in to comment.