acts_as_decryptable 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,12 +6,12 @@ module ActiveRecord
6
6
  end
7
7
 
8
8
  # class SomeModel < ActiveRecord::Base
9
- # acts_as_decryptable :encrypt => [:name, :email], :key => "SOME_KEY"
9
+ # acts_as_decryptable :encrypt => [:name, :email], :key => "SOME_KEY", :show => '*' * 8
10
10
  # end
11
11
  #
12
12
  # some_model_object.name = "name"
13
13
  # some_model_object.save
14
- # some_model_object.name # => "encrypted name"
14
+ # some_model_object.name # => "encrypted name" or "********"
15
15
  # some_model_object.decrypted_name # => "name"
16
16
  module ClassMethods
17
17
  def acts_as_decryptable(options)
@@ -23,14 +23,23 @@ module ActiveRecord
23
23
  configuration[:encrypt].each do |field_name|
24
24
  class_eval <<-EOV
25
25
  def #{field_name}=(value)
26
- self[:#{field_name}] = SymmetricCrypto.encrypt(value, #{configuration[:key].to_json})
26
+ if value != #{configuration[:show].to_json}
27
+ self[:#{field_name}] = SymmetricCrypto.encrypt(value, #{configuration[:key].to_json})
28
+ end
27
29
  end
28
30
 
29
31
  def decrypt_#{field_name}
30
32
  SymmetricCrypto.decrypt(self[:#{field_name}], #{configuration[:key].to_json})
31
33
  end
32
-
33
34
  EOV
35
+
36
+ if configuration[:show]
37
+ class_eval <<-EOV
38
+ def #{field_name}
39
+ #{configuration[:show].to_json}
40
+ end
41
+ EOV
42
+ end
34
43
  end
35
44
 
36
45
  class_eval <<-EOV
@@ -30,7 +30,7 @@ class Mixin < ActiveRecord::Base
30
30
  end
31
31
 
32
32
  class DecryptableMixin < Mixin
33
- acts_as_decryptable :encrypt => [ :encrypted ], :key => 'a' * 32
33
+ acts_as_decryptable :encrypt => [ :encrypted ], :key => 'a' * 32, :show => '*' * 8
34
34
 
35
35
  def self.table_name() "mixins" end
36
36
  end
@@ -52,8 +52,13 @@ class DecryptTest < Test::Unit::TestCase
52
52
  decryptable.encrypted = "text"
53
53
  decryptable.save!
54
54
 
55
- assert_equal "text", decryptable.plaintext
56
- assert_equal "text", decryptable.decrypt_encrypted
57
- assert_not_equal "text", decryptable.encrypted
55
+ assert_equal "text", decryptable.plaintext
56
+ assert_equal "text", decryptable.decrypt_encrypted
57
+ assert_equal "*" * 8, decryptable.encrypted
58
+
59
+ decryptable.encrypted = "*" * 8
60
+ decryptable.save
61
+ assert_equal "text", decryptable.decrypt_encrypted
62
+ assert_equal "*" * 8, decryptable.encrypted
58
63
  end
59
64
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_decryptable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - hc5duke