Skip to content
Browse files

add provisions for a strict_mode distinction to the AES encryptor

  • Loading branch information...
1 parent 3769caf commit aadef332de11789f5b93c648b9703171a95bfd3d @Capncavedan Capncavedan committed Mar 8, 2013
Showing with 59 additions and 24 deletions.
  1. +23 −8 lib/crypt_keeper/provider/aes.rb
  2. +36 −16 spec/provider/aes_spec.rb
View
31 lib/crypt_keeper/provider/aes.rb
@@ -13,6 +13,9 @@ class Aes
# Public: An instance of OpenSSL::Cipher::Cipher
attr_accessor :aes
+ # Public: Whether blank string is accepted as data for encryption
+ attr_accessor :strict_mode
+
# Public: Initializes the class
#
# options - A hash of options. :key is required
@@ -24,27 +27,39 @@ def initialize(options = {})
raise ArgumentError, "Missing :key"
end
+ @strict_mode = options.fetch(:strict_mode) do
+ @strict_mode = true
+ end
+
@key = Digest::SHA256.digest(key)
end
# Public: Encrypt a string
#
# Returns a string
def encrypt(value)
- aes.encrypt
- aes.key = key
- Base64::encode64("#{aes.random_iv}#{SEPARATOR}#{aes.update(value.to_s) + aes.final}")
+ if value == '' && !strict_mode
+ value
+ else
+ aes.encrypt
+ aes.key = key
+ Base64::encode64("#{aes.random_iv}#{SEPARATOR}#{aes.update(value.to_s) + aes.final}")
+ end
end
# Public: Decrypt a string
#
# Returns a string
def decrypt(value)
- iv, value = Base64::decode64(value.to_s).split(SEPARATOR)
- aes.decrypt
- aes.key = key
- aes.iv = iv
- aes.update(value) + aes.final
+ if value == '' && !strict_mode
+ value
+ else
+ iv, value = Base64::decode64(value.to_s).split(SEPARATOR)
+ aes.decrypt
+ aes.key = key
+ aes.iv = iv
+ aes.update(value) + aes.final
+ end
end
end
end
View
52 spec/provider/aes_spec.rb
@@ -3,32 +3,52 @@
module CryptKeeper
module Provider
describe Aes do
- subject { Aes.new(key: 'cake') }
+ context "strict mode (default)" do
+ subject { Aes.new(key: 'cake') }
- describe "#initialize" do
- let(:hexed_key) do
- Digest::SHA256.digest('cake')
+ describe "#initialize" do
+ let(:hexed_key) do
+ Digest::SHA256.digest('cake')
+ end
+
+ its(:key) { should == hexed_key }
+ its(:strict_mode) { should be_true }
+ specify { expect { Aes.new }.to raise_error(ArgumentError, "Missing :key") }
end
- its(:key) { should == hexed_key }
- specify { expect { Aes.new }.to raise_error(ArgumentError, "Missing :key") }
- end
+ describe "#encrypt" do
+ let(:encrypted) do
+ subject.encrypt 'string'
+ end
- describe "#encrypt" do
- let(:encrypted) do
- subject.encrypt 'string'
+ specify { encrypted.should_not == 'string' }
+ specify { encrypted.should_not be_blank }
+ specify { expect { subject.encrypt('') }.to raise_error(ArgumentError, "data must not be empty") }
end
- specify { encrypted.should_not == 'string' }
- specify { encrypted.should_not be_blank }
+ describe "#decrypt" do
+ let(:decrypted) do
+ subject.decrypt "MC41MDk5MjI2NjgxMDI1MDI2OmNyeXB0X2tlZXBlcjpPI/8dCqWXDMVj7Jqs\nuwf/\n"
+ end
+
+ specify { decrypted.should == 'string' }
+
+ specify { expect { subject.decrypt('') }.to raise_error(TypeError, "can't convert nil into String") }
+ end
end
- describe "#decrypt" do
- let(:decrypted) do
- subject.decrypt "MC41MDk5MjI2NjgxMDI1MDI2OmNyeXB0X2tlZXBlcjpPI/8dCqWXDMVj7Jqs\nuwf/\n"
+ context "not-strict mode" do
+ subject { Aes.new(key: 'cake', strict_mode: false) }
+
+ its(:strict_mode) { should be_false }
+
+ describe "#encrypt" do
+ specify { expect { subject.encrypt('') }.not_to raise_error(ArgumentError) }
end
- specify { decrypted.should == 'string' }
+ describe "#decrypt" do
+ specify { expect { subject.decrypt('') }.not_to raise_error(TypeError) }
+ end
end
end
end

0 comments on commit aadef33

Please sign in to comment.
Something went wrong with that request. Please try again.