-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace AesNew provider with ActiveSupport::MessageEncryptor
- Loading branch information
1 parent
ea5ce8b
commit 0b60ab2
Showing
13 changed files
with
109 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require "active_support/message_encryptor" | ||
|
||
module CryptKeeper | ||
module Provider | ||
class ActiveSupport < Base | ||
attr_reader :encryptor | ||
|
||
# Public: Initializes the encryptor | ||
# | ||
# options - A hash, :key and :salt are required | ||
# | ||
# Returns nothing. | ||
def initialize(options = {}) | ||
key = options.fetch(:key) | ||
salt = options.fetch(:salt) | ||
|
||
@encryptor = ::ActiveSupport::MessageEncryptor.new \ | ||
::ActiveSupport::KeyGenerator.new(key).generate_key(salt, 32) | ||
end | ||
|
||
# Public: Encrypts a string | ||
# | ||
# value - Plaintext value | ||
# | ||
# Returns an encrypted string | ||
def encrypt(value) | ||
encryptor.encrypt_and_sign(value) | ||
end | ||
|
||
# Public: Decrypts a string | ||
# | ||
# value - Cipher text | ||
# | ||
# Returns a plaintext string | ||
def decrypt(value) | ||
encryptor.decrypt_and_verify(value) | ||
end | ||
|
||
# Public: Searches the table | ||
# | ||
# records - ActiveRecord::Relation | ||
# field - Field name to match | ||
# criteria - Value to match | ||
# | ||
# Returns an Enumerable | ||
def search(records, field, criteria) | ||
records.select { |record| record[field] == criteria } | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'spec_helper' | ||
|
||
describe CryptKeeper::Provider::ActiveSupport do | ||
subject { described_class.new(key: 'cake', salt: 'salt') } | ||
|
||
let :plaintext do | ||
"string" | ||
end | ||
|
||
let :encrypted do | ||
subject.encrypt plaintext | ||
end | ||
|
||
let :decrypted do | ||
subject.decrypt encrypted | ||
end | ||
|
||
describe "#encrypt" do | ||
specify { expect(encrypted).to_not eq(plaintext) } | ||
specify { expect(encrypted).to_not be_blank } | ||
end | ||
|
||
describe "#decrypt" do | ||
specify { expect(decrypted).to eq(plaintext) } | ||
specify { expect(decrypted).to_not be_blank } | ||
end | ||
|
||
describe "#search" do | ||
let :records do | ||
[{ name: 'Bob' }, { name: 'Tim' }] | ||
end | ||
|
||
it "finds the matching record" do | ||
expect(subject.search(records, :name, 'Bob')).to eql([records.first]) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.