unidom-common 1.7.1 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/models/unidom/common/concerns/secure_column.rb +33 -29
- data/lib/unidom/common/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28a8fc2b74ee9eec8f6e875a18e986da12400624
|
4
|
+
data.tar.gz: f1834cc03640dffc69917c2e14e44677355ca9ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d12d9206fe0ebd406d80df3184380081407a7bde2bc32a6f4cf7fb9da495379c968f1e3c3f17cff6c9153c56c2467b51f86c3050ba928ff93c12a81c5467a494
|
7
|
+
data.tar.gz: 327c616410e7d17a5d5afbb1caad676f85c833e236b72f2755a88272ed4651882a1223f872a28bc72517a2fc237831329c83ee4e274cda4bd690afb156009754
|
@@ -8,14 +8,43 @@ module Unidom::Common::Concerns::SecureColumn
|
|
8
8
|
|
9
9
|
cattr_accessor :secure_columns
|
10
10
|
|
11
|
+
def do_encrypt_secure_column(name)
|
12
|
+
name = name.to_s
|
13
|
+
content = { 'nonce' => SecureRandom.hex(8), 'timestamp' => Time.now.to_i }
|
14
|
+
secure_columns[name].each do |field| content[field.to_s] = send(field) end
|
15
|
+
content = content.sort.to_h.to_json
|
16
|
+
aes_key = Digest::SHA512::digest self.class.exact_signature(self.class, name, '')
|
17
|
+
encoded = hex_encrypt content, key: aes_key
|
18
|
+
json = {
|
19
|
+
encoded: encoded,
|
20
|
+
signature: Unidom::Common::Numeration.hex(self.class.exact_signature self.class, name, content),
|
21
|
+
encryption_algorithm: self.class.encryption_algorithm
|
22
|
+
}
|
23
|
+
send "#{name}=", json
|
24
|
+
end
|
25
|
+
|
26
|
+
def do_decrypt_secure_column(name)
|
27
|
+
name = name.to_sym
|
28
|
+
return unless respond_to? name
|
29
|
+
json = send(name)
|
30
|
+
return if json['encoded'].blank?||json['signature'].blank?||json['encryption_algorithm'].blank?
|
31
|
+
return if self.class.encryption_algorithm!=json['encryption_algorithm']
|
32
|
+
aes_key = Digest::SHA512::digest self.class.exact_signature(self.class, name, '')
|
33
|
+
content = decrypt Unidom::Common::Numeration.rev_hex(json['encoded']), key: aes_key
|
34
|
+
actual_signature = self.class.exact_signature(self.class, name, content)
|
35
|
+
return if Unidom::Common::Numeration.rev_hex(json['signature'])!=actual_signature
|
36
|
+
parsed = JSON.parse content
|
37
|
+
parsed.each do |key, value| send "#{key}=", value unless [ 'nonce', 'timestamp' ].include? key end
|
38
|
+
end
|
39
|
+
|
11
40
|
end
|
12
41
|
|
13
42
|
module ClassMethods
|
14
43
|
|
15
44
|
def secure_column(name, fields: [])
|
16
45
|
|
17
|
-
name
|
18
|
-
secure_columns = secure_columns||{}
|
46
|
+
name = name.to_s
|
47
|
+
self.secure_columns = self.secure_columns||{}
|
19
48
|
if secure_columns[name].present?
|
20
49
|
raise ArgumentError.new("The #{name} column was defined as a secure column already.")
|
21
50
|
else
|
@@ -24,33 +53,8 @@ module Unidom::Common::Concerns::SecureColumn
|
|
24
53
|
fields.each do |field| attr_accessor field.to_sym if columns_hash[field.to_s].blank? end
|
25
54
|
|
26
55
|
instance_eval do
|
27
|
-
|
28
|
-
|
29
|
-
content = { 'nonce' => SecureRandom.hex(8), 'timestamp' => Time.now.to_i }
|
30
|
-
secure_columns[name].each do |field| content[field.to_s] = send(field) end
|
31
|
-
content = content.sort.to_h.to_json
|
32
|
-
aes_key = Digest::SHA512::digest self.class.exact_signature(self.class, name, '')
|
33
|
-
encoded = hex_encrypt content, key: aes_key
|
34
|
-
json = {
|
35
|
-
encoded: encoded,
|
36
|
-
signature: Unidom::Common::Numeration.hex(self.class.exact_signature self.class, name, content),
|
37
|
-
encryption_algorithm: self.class.encryption_algorithm
|
38
|
-
}
|
39
|
-
send "#{name}=", json
|
40
|
-
end
|
41
|
-
|
42
|
-
after_find do
|
43
|
-
json = send(name)
|
44
|
-
return if json['encoded'].blank?||json['signature'].blank?||json['encryption_algorithm'].blank?
|
45
|
-
return if self.class.encryption_algorithm!=json['encryption_algorithm']
|
46
|
-
aes_key = Digest::SHA512::digest self.class.exact_signature(self.class, name, '')
|
47
|
-
content = decrypt Unidom::Common::Numeration.rev_hex(json['encoded']), key: aes_key
|
48
|
-
actual_signature = self.class.exact_signature(self.class, name, content)
|
49
|
-
return if Unidom::Common::Numeration.rev_hex(json['signature'])!=actual_signature
|
50
|
-
parsed = JSON.parse content
|
51
|
-
parsed.each do |key, value| send "#{key}=", value unless [ 'nonce', 'timestamp' ].include? key end
|
52
|
-
end
|
53
|
-
|
56
|
+
before_save do do_encrypt_secure_column name end
|
57
|
+
after_find do do_decrypt_secure_column name.to_sym end
|
54
58
|
end
|
55
59
|
|
56
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unidom-common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.7.
|
4
|
+
version: 1.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Topbit Du
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|