Skip to content
Open
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
7 changes: 4 additions & 3 deletions lib/attr_encrypted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,13 @@ def decrypt(attribute, encrypted_value, options = {})
encrypted_value = encrypted_value.unpack(options[:encode]).first if options[:encode]
value = options[:encryptor].send(options[:decrypt_method], options.merge!(value: encrypted_value))
if options[:marshal]
value = options[:marshaler].send(options[:load_method], value)
options[:marshaler].send(options[:load_method], value)
elsif defined?(Encoding)
encoding = Encoding.default_internal || Encoding.default_external
value = value.force_encoding(encoding.name)
value.dup.force_encoding(encoding.name)
else
value
end
value
else
encrypted_value
end
Expand Down
20 changes: 19 additions & 1 deletion test/attr_encrypted_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ def self.silly_decrypt(options)
end
end

class FreezingDecryptor
def self.frozen_encrypt(options)
options[:value].freeze
end

def self.frozen_decrypt(options)
options[:value].freeze
end
end

class User
extend AttrEncrypted
self.attr_encrypted_options[:key] = Proc.new { |user| SECRET_KEY } # default key
Expand All @@ -22,6 +32,7 @@ class User
attr_encrypted :password, :prefix => 'crypted_', :suffix => '_test'
attr_encrypted :ssn, :key => :secret_key, :attribute => 'ssn_encrypted'
attr_encrypted :credit_card, :encryptor => SillyEncryptor, :encrypt_method => :silly_encrypt, :decrypt_method => :silly_decrypt, :some_arg => 'test'
attr_encrypted :frozen_string, :encryptor => FreezingDecryptor, :encrypt_method => :frozen_encrypt, :decrypt_method => :frozen_decrypt
attr_encrypted :with_encoding, :key => SECRET_KEY, :encode => true
attr_encrypted :with_custom_encoding, :key => SECRET_KEY, :encode => 'm'
attr_encrypted :with_marshaling, :key => SECRET_KEY, :marshal => true
Expand All @@ -37,7 +48,8 @@ class User
attr_accessor :salt
attr_accessor :should_encrypt

def initialize(email: nil)
def initialize(email: nil, frozen_string: nil)
self.frozen_string = frozen_string
self.email = email
self.salt = Time.now.to_i.to_s
self.should_encrypt = true
Expand Down Expand Up @@ -374,6 +386,12 @@ def test_should_not_generate_salt_per_attribute_by_default
assert_nil thing.encrypted_email_salt
end

def test_should_decrypt_and_encode_frozen_strings
user = User.new(frozen_string: 'test')
result = user.decrypt(:frozen_string, user.encrypted_frozen_string)
assert_equal 'test', result
end

def test_should_decrypt_second_record
@user1 = User.new
@user1.email = 'test@example.com'
Expand Down