Skip to content

Commit

Permalink
Don't attempt to encrypt/decrypt empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
shuber committed Jan 18, 2009
1 parent bcfafe5 commit b0a701f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
@@ -1,3 +1,6 @@
2009-01-18 - Sean Huber (shuber@huberry.com)
* Don't attempt to encrypt/decrypt empty strings

2009-01-14 - Sean Huber (shuber@huberry.com)
* Move Class logic into Object

Expand Down
2 changes: 1 addition & 1 deletion attr_encrypted.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'attr_encrypted'
s.version = '1.0.7'
s.version = '1.0.8'
s.date = '2009-01-13'

s.summary = 'Generates attr_accessors that encrypt and decrypt attributes transparently'
Expand Down
8 changes: 4 additions & 4 deletions lib/attr_encrypted.rb
Expand Up @@ -152,8 +152,8 @@ def attr_encrypted(*attrs)

define_class_method "encrypt_#{attribute}" do |value|
if options[:if] && !options[:unless]
if value.nil?
encrypted_value = nil
if value.nil? || (value.is_a?(String) && value.empty?)
encrypted_value = value
else
value = Marshal.dump(value) if options[:marshal]
encrypted_value = options[:encryptor].send options[:encrypt_method], options.merge(:value => value)
Expand All @@ -167,8 +167,8 @@ def attr_encrypted(*attrs)

define_class_method "decrypt_#{attribute}" do |encrypted_value|
if options[:if] && !options[:unless]
if encrypted_value.nil?
decrypted_value = nil
if encrypted_value.nil? || (encrypted_value.is_a?(String) && encrypted_value.empty?)
decrypted_value = encrypted_value
else
encrypted_value = encrypted_value.unpack(options[:encode]).to_s if options[:encode]
decrypted_value = options[:encryptor].send(options[:decrypt_method], options.merge(:value => encrypted_value))
Expand Down
8 changes: 8 additions & 0 deletions test/attr_encrypted_test.rb
Expand Up @@ -75,6 +75,10 @@ def test_should_not_encrypt_nil_value
assert_nil User.encrypt_email(nil)
end

def test_should_not_encrypt_empty_string
assert_equal '', User.encrypt_email('')
end

def test_should_encrypt_email
assert_not_nil User.encrypt_email('test@example.com')
assert_not_equal 'test@example.com', User.encrypt_email('test@example.com')
Expand All @@ -92,6 +96,10 @@ def test_should_not_decrypt_nil_value
assert_nil User.decrypt_email(nil)
end

def test_should_not_decrypt_empty_string
assert_equal '', User.decrypt_email('')
end

def test_should_decrypt_email
encrypted_email = User.encrypt_email('test@example.com')
assert_not_equal 'test@test.com', encrypted_email
Expand Down

0 comments on commit b0a701f

Please sign in to comment.