Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add decrypt table (the reverse of encrypt table) #87

Merged
merged 7 commits into from Feb 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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