unidom-common 1.10 → 1.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/app/models/unidom/common/concerns/aes256_cryptor.rb +22 -2
- data/app/models/unidom/common/concerns/argument_validation.rb +3 -0
- data/app/models/unidom/common/concerns/exact_column.rb +5 -0
- data/app/models/unidom/common/concerns/md5_digester.rb +30 -0
- data/app/models/unidom/common/concerns/model_extension.rb +11 -0
- data/app/models/unidom/common/concerns/notation_column.rb +14 -0
- data/app/models/unidom/common/concerns/secure_column.rb +18 -0
- data/app/models/unidom/common/concerns/sha1_digester.rb +20 -0
- data/app/models/unidom/common/concerns/sha256_digester.rb +20 -0
- data/app/models/unidom/common/concerns/sha2_digester.rb +20 -0
- data/app/models/unidom/common/concerns/sha384_digester.rb +20 -0
- data/app/models/unidom/common/concerns/sha512_digester.rb +20 -0
- data/db/migrate/20000101000010_enable_pgcrypto_extension.rb +7 -0
- data/lib/unidom/common.rb +2 -0
- data/lib/unidom/common/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d9cd0cf5814089ba939160bb2ad58af74acfab2
|
4
|
+
data.tar.gz: e926c6a1f817e200cab79619d80e6fe7be0eef07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7e967c73037be4d337f418e70dfd9715bc7e067ba9bd3ba754a452ae7788458ed91ffe66324eb1b8d1d8d50cdfde34970ed9733849bb7f656e25dc05c706123
|
7
|
+
data.tar.gz: 9dd384ddeecbcb0cce714086a9d28246d8c969b07a930a933815079e5f3cbdaa4a4231612f2e335d6d395596a36a1198ff4b88e725b4d63553358ed8b0a9a070
|
data/README.md
CHANGED
@@ -18,8 +18,6 @@ module Unidom::Common::Concerns::Aes256Cryptor
|
|
18
18
|
|
19
19
|
##
|
20
20
|
# 将密文 encoded 用秘钥 key 进行解密。如:
|
21
|
-
# decrypt encoded
|
22
|
-
# 或
|
23
21
|
# decrypt encoded, key: aes256_key
|
24
22
|
def decrypt(encoded, key: nil)
|
25
23
|
self.class.decrypt encoded, key: key
|
@@ -29,10 +27,18 @@ module Unidom::Common::Concerns::Aes256Cryptor
|
|
29
27
|
self.class.aes_256_padding
|
30
28
|
end
|
31
29
|
|
30
|
+
##
|
31
|
+
# 将明文 message 用秘钥 key 进行加密,并转换成16进制表达。如:
|
32
|
+
# hex_encrypt 'clear text', key: aes256_key
|
32
33
|
def hex_encrypt(message, key: nil)
|
33
34
|
self.class.hex_encrypt message, key: key
|
34
35
|
end
|
35
36
|
|
37
|
+
##
|
38
|
+
# 将明文 message 用秘钥 key 进行解密,并转换成16进制表达。如:
|
39
|
+
# hex_decrypt 'clear text'
|
40
|
+
# 或
|
41
|
+
# hex_decrypt 'clear text', key: aes256_key
|
36
42
|
def hex_decrypt(encoded, key: nil)
|
37
43
|
self.class.hex_decrypt encoded, key: key
|
38
44
|
end
|
@@ -45,6 +51,11 @@ module Unidom::Common::Concerns::Aes256Cryptor
|
|
45
51
|
'AES-256-CBC'
|
46
52
|
end
|
47
53
|
|
54
|
+
##
|
55
|
+
# 将明文 message 用秘钥 key 进行加密。如:
|
56
|
+
# encrypt 'clear text'
|
57
|
+
# 或
|
58
|
+
# encrypt 'clear text', key: aes256_key
|
48
59
|
def encrypt(message, key: nil)
|
49
60
|
|
50
61
|
raise ArgumentError.new('The message argument is required.') if message.blank?
|
@@ -59,6 +70,9 @@ module Unidom::Common::Concerns::Aes256Cryptor
|
|
59
70
|
|
60
71
|
end
|
61
72
|
|
73
|
+
##
|
74
|
+
# 将密文 encoded 用秘钥 key 进行解密。如:
|
75
|
+
# decrypt encoded, key: aes256_key
|
62
76
|
def decrypt(encoded, key: nil)
|
63
77
|
|
64
78
|
raise ArgumentError.new('The encoded argument is required.') if encoded.blank?
|
@@ -77,10 +91,16 @@ module Unidom::Common::Concerns::Aes256Cryptor
|
|
77
91
|
respond_to?(:cryption_padding) ? cryption_padding : 9
|
78
92
|
end
|
79
93
|
|
94
|
+
##
|
95
|
+
# 将明文 message 用秘钥 key 进行加密,并转换成16进制表达。如:
|
96
|
+
# self.hex_encrypt 'clear text', key: aes256_key
|
80
97
|
def hex_encrypt(message, key: nil)
|
81
98
|
Unidom::Common::Numeration.hex encrypt(message, key: key)
|
82
99
|
end
|
83
100
|
|
101
|
+
##
|
102
|
+
# 将明文 message 用秘钥 key 进行解密,并转换成16进制表达。如:
|
103
|
+
# self.hex_decrypt 'clear text', key: aes256_key
|
84
104
|
def hex_decrypt(encoded, key: nil)
|
85
105
|
Unidom::Common::Numeration.hex decrypt(Unidom::Common::Numeration.rev_hex(encoded), key: key)
|
86
106
|
end
|
@@ -18,6 +18,9 @@ module Unidom::Common::Concerns::ArgumentValidation
|
|
18
18
|
|
19
19
|
module ClassMethods
|
20
20
|
|
21
|
+
##
|
22
|
+
# 断言给定的参数 value 非空。如果为空,则抛出 ArgumentError 异常。如:
|
23
|
+
# assert_present! :person, person
|
21
24
|
def assert_present!(name, value)
|
22
25
|
raise ArgumentError.new("The #{name} argument is required.") if value.blank?
|
23
26
|
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
##
|
2
|
+
# Exact Column 为加密数据提供摘要索引。
|
3
|
+
|
1
4
|
module Unidom::Common::Concerns::ExactColumn
|
2
5
|
|
3
6
|
extend ActiveSupport::Concern
|
@@ -31,6 +34,8 @@ module Unidom::Common::Concerns::ExactColumn
|
|
31
34
|
|
32
35
|
end
|
33
36
|
|
37
|
+
##
|
38
|
+
# 计算精确索引列的值。此方法被 exact_column 方法调用。
|
34
39
|
def exact_signature(klass, name, value, secret_key_base: Rails.application.secrets[:secret_key_base])
|
35
40
|
text = "#{secret_key_base}/#{klass.table_name}##{name}=#{value}"
|
36
41
|
#text = "#{Rails.application.secrets[:secret_key_base]}@#{Rails.root}/#{klass.table_name}##{name}=#{value}"
|
@@ -21,6 +21,16 @@ module Unidom::Common::Concerns::Md5Digester
|
|
21
21
|
self.class.digest message, pepper: pepper
|
22
22
|
end
|
23
23
|
|
24
|
+
##
|
25
|
+
# 对明文 message 进行 MD-5 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
26
|
+
# class SomeModel
|
27
|
+
# include Unidom::Common::Concerns::Md5Digester
|
28
|
+
# def some_method(param_1)
|
29
|
+
# hex_digest param_1
|
30
|
+
# # 或者
|
31
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
32
|
+
# end
|
33
|
+
# end
|
24
34
|
def hex_digest(message, pepper: nil)
|
25
35
|
self.class.hex_digest message, pepper: pepper
|
26
36
|
end
|
@@ -29,10 +39,30 @@ module Unidom::Common::Concerns::Md5Digester
|
|
29
39
|
|
30
40
|
module ClassMethods
|
31
41
|
|
42
|
+
##
|
43
|
+
# 对明文 message 进行 MD-5 摘要, pepper 是用于增加混乱的内容。如:
|
44
|
+
# class SomeModel
|
45
|
+
# include Unidom::Common::Concerns::Md5Digester
|
46
|
+
# def self.some_method(param_1)
|
47
|
+
# digest param_1
|
48
|
+
# # 或者
|
49
|
+
# digest param_1, pepper: 'my_pepper'
|
50
|
+
# end
|
51
|
+
# end
|
32
52
|
def digest(message, pepper: nil)
|
33
53
|
message.present? ? Digest::MD5.digest("#{message}_#{Rails.application.secrets[:secret_key_base]}_#{pepper}") : nil
|
34
54
|
end
|
35
55
|
|
56
|
+
##
|
57
|
+
# 对明文 message 进行 MD-5 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
58
|
+
# class SomeModel
|
59
|
+
# include Unidom::Common::Concerns::Md5Digester
|
60
|
+
# def self.some_method(param_1)
|
61
|
+
# hex_digest param_1
|
62
|
+
# # 或者
|
63
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
64
|
+
# end
|
65
|
+
# end
|
36
66
|
def hex_digest(message, pepper: nil)
|
37
67
|
message.present? ? Unidom::Common::Numeration.hex(digest(message, pepper: pepper)) : nil
|
38
68
|
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
##
|
2
|
+
# Model Extension 是通用的模型扩展关注点,提供参数验证、JSON 注解字段、加密字段等。
|
3
|
+
|
1
4
|
module Unidom::Common::Concerns::ModelExtension
|
2
5
|
|
3
6
|
extend ActiveSupport::Concern
|
@@ -115,6 +118,10 @@ module Unidom::Common::Concerns::ModelExtension
|
|
115
118
|
#relation
|
116
119
|
end
|
117
120
|
|
121
|
+
##
|
122
|
+
# 对当前对象进行软删除。软删除之后, #closed_at 会被置为当前时间, #defunct 会被置为 false 。并且对象会被自动保存。如:
|
123
|
+
# model = YourModel.find your_id
|
124
|
+
# model.soft_destroy
|
118
125
|
def soft_destroy
|
119
126
|
self.closed_at = Time.now
|
120
127
|
self.defunct = true
|
@@ -143,6 +150,10 @@ module Unidom::Common::Concerns::ModelExtension
|
|
143
150
|
model.respond_to?(:id) ? model.id : model
|
144
151
|
end
|
145
152
|
|
153
|
+
##
|
154
|
+
# 将模型对象或者 code 转换成 code 。如:
|
155
|
+
# to_id(category) # category.id
|
156
|
+
# to_id(category.code) # category.id
|
146
157
|
def to_code(code)
|
147
158
|
code.respond_to?(:code) ? code.code : code
|
148
159
|
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
##
|
2
|
+
# Notation Column 提供定义 JSON 注解列的方法。
|
3
|
+
|
1
4
|
module Unidom::Common::Concerns::NotationColumn
|
2
5
|
|
3
6
|
extend ActiveSupport::Concern
|
@@ -35,6 +38,17 @@ module Unidom::Common::Concerns::NotationColumn
|
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
41
|
+
##
|
42
|
+
# 定义标注字段的 boolean 型的列。如:
|
43
|
+
# class YourModel < ApplicationRecord
|
44
|
+
# include Unidom::Common::Concerns::NotationColumn
|
45
|
+
# notation_boolean_column :still_alive
|
46
|
+
# end
|
47
|
+
# 则为 YourModel 的实例生成2个方法:still_alive? 和 still_alive=。
|
48
|
+
# model = YourModel.new still_alive: true
|
49
|
+
# model.still_alive? # true
|
50
|
+
# model.still_alive = false
|
51
|
+
# model.still_alive? # false
|
38
52
|
def notation_boolean_column(*names)
|
39
53
|
names.each do |name|
|
40
54
|
name = name.to_s
|
@@ -1,3 +1,5 @@
|
|
1
|
+
##
|
2
|
+
# Secure Column 提供定义加密列的方法。
|
1
3
|
module Unidom::Common::Concerns::SecureColumn
|
2
4
|
|
3
5
|
extend ActiveSupport::Concern
|
@@ -41,6 +43,22 @@ module Unidom::Common::Concerns::SecureColumn
|
|
41
43
|
|
42
44
|
module ClassMethods
|
43
45
|
|
46
|
+
##
|
47
|
+
# 配置加密列。如:
|
48
|
+
# class YourModel < ApplicationRecord
|
49
|
+
# attr_accessor :identity_card_name, :identity_card_address, :passport_name, :passport_address
|
50
|
+
# include Unidom::Common::Concerns::SecureColumn
|
51
|
+
# secure_column :secure_identity_card, fields: [ :identity_card_name, :identity_card_address ]
|
52
|
+
# secure_column :secure_passport, fields: [ :passport_name, :passport_address ]
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# model = YourModel.create! identity_card_name: '张三', identity_card_address: '地址1',
|
56
|
+
# passport_name: '李四', passport_address: '地址2'
|
57
|
+
# #identity_card_name 和 #identity_card_address 会被加密后保存到 #secure_identity_card 字段中。
|
58
|
+
# #passport_name 和 #passport_address 会被加密后保存到 #secure_passport 字段中。
|
59
|
+
# model_2 = YourModel.find model.id
|
60
|
+
# model_2.identity_card_name # 张三
|
61
|
+
# #secure_identity_card 中存储的加密信息会被自动解密,并存储到 #identity_card_name 和 #identity_card_address 中。
|
44
62
|
def secure_column(name, fields: [])
|
45
63
|
|
46
64
|
name = name.to_s
|
@@ -39,10 +39,30 @@ module Unidom::Common::Concerns::Sha1Digester
|
|
39
39
|
|
40
40
|
module ClassMethods
|
41
41
|
|
42
|
+
##
|
43
|
+
# 对明文 message 进行 SHA-1 摘要, pepper 是用于增加混乱的内容。如:
|
44
|
+
# class SomeModel
|
45
|
+
# include Unidom::Common::Concerns::Sha1Digester
|
46
|
+
# def self.some_method(param_1)
|
47
|
+
# digest param_1
|
48
|
+
# # 或者
|
49
|
+
# digest param_1, pepper: 'my_pepper'
|
50
|
+
# end
|
51
|
+
# end
|
42
52
|
def digest(message, pepper: nil)
|
43
53
|
message.present? ? Digest::SHA1.digest("#{message}_#{Rails.application.secrets[:secret_key_base]}_#{pepper}") : nil
|
44
54
|
end
|
45
55
|
|
56
|
+
##
|
57
|
+
# 对明文 message 进行 SHA-1 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
58
|
+
# class SomeModel
|
59
|
+
# include Unidom::Common::Concerns::Sha1Digester
|
60
|
+
# def self.some_method(param_1)
|
61
|
+
# hex_digest param_1
|
62
|
+
# # 或者
|
63
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
64
|
+
# end
|
65
|
+
# end
|
46
66
|
def hex_digest(message, pepper: nil)
|
47
67
|
message.present? ? Unidom::Common::Numeration.hex(digest(message, pepper: pepper)) : nil
|
48
68
|
end
|
@@ -39,10 +39,30 @@ module Unidom::Common::Concerns::Sha256Digester
|
|
39
39
|
|
40
40
|
module ClassMethods
|
41
41
|
|
42
|
+
##
|
43
|
+
# 对明文 message 进行 SHA-256 摘要, pepper 是用于增加混乱的内容。如:
|
44
|
+
# class SomeModel
|
45
|
+
# include Unidom::Common::Concerns::Sha256Digester
|
46
|
+
# def self.some_method(param_1)
|
47
|
+
# digest param_1
|
48
|
+
# # 或者
|
49
|
+
# digest param_1, pepper: 'my_pepper'
|
50
|
+
# end
|
51
|
+
# end
|
42
52
|
def digest(message, pepper: nil)
|
43
53
|
message.present? ? Digest::SHA256.digest("#{message}_#{Rails.application.secrets[:secret_key_base]}_#{pepper}") : nil
|
44
54
|
end
|
45
55
|
|
56
|
+
##
|
57
|
+
# 对明文 message 进行 SHA-256 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
58
|
+
# class SomeModel
|
59
|
+
# include Unidom::Common::Concerns::Sha256Digester
|
60
|
+
# def self.some_method(param_1)
|
61
|
+
# hex_digest param_1
|
62
|
+
# # 或者
|
63
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
64
|
+
# end
|
65
|
+
# end
|
46
66
|
def hex_digest(message, pepper: nil)
|
47
67
|
message.present? ? Unidom::Common::Numeration.hex(digest(message, pepper: pepper)) : nil
|
48
68
|
end
|
@@ -39,10 +39,30 @@ module Unidom::Common::Concerns::Sha2Digester
|
|
39
39
|
|
40
40
|
module ClassMethods
|
41
41
|
|
42
|
+
##
|
43
|
+
# 对明文 message 进行 SHA-2 摘要, pepper 是用于增加混乱的内容。如:
|
44
|
+
# class SomeModel
|
45
|
+
# include Unidom::Common::Concerns::Sha2Digester
|
46
|
+
# def self.some_method(param_1)
|
47
|
+
# digest param_1
|
48
|
+
# # 或者
|
49
|
+
# digest param_1, pepper: 'my_pepper'
|
50
|
+
# end
|
51
|
+
# end
|
42
52
|
def digest(message, pepper: nil)
|
43
53
|
message.present? ? Digest::SHA2.digest("#{message}_#{Rails.application.secrets[:secret_key_base]}_#{pepper}") : nil
|
44
54
|
end
|
45
55
|
|
56
|
+
##
|
57
|
+
# 对明文 message 进行 SHA-2 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
58
|
+
# class SomeModel
|
59
|
+
# include Unidom::Common::Concerns::Sha2Digester
|
60
|
+
# def self.some_method(param_1)
|
61
|
+
# hex_digest param_1
|
62
|
+
# # 或者
|
63
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
64
|
+
# end
|
65
|
+
# end
|
46
66
|
def hex_digest(message, pepper: nil)
|
47
67
|
message.present? ? Unidom::Common::Numeration.hex(digest(message, pepper: pepper)) : nil
|
48
68
|
end
|
@@ -39,10 +39,30 @@ module Unidom::Common::Concerns::Sha384Digester
|
|
39
39
|
|
40
40
|
module ClassMethods
|
41
41
|
|
42
|
+
##
|
43
|
+
# 对明文 message 进行 SHA-384 摘要, pepper 是用于增加混乱的内容。如:
|
44
|
+
# class SomeModel
|
45
|
+
# include Unidom::Common::Concerns::Sha384Digester
|
46
|
+
# def self.some_method(param_1)
|
47
|
+
# digest param_1
|
48
|
+
# # 或者
|
49
|
+
# digest param_1, pepper: 'my_pepper'
|
50
|
+
# end
|
51
|
+
# end
|
42
52
|
def digest(message, pepper: nil)
|
43
53
|
message.present? ? Digest::SHA384.digest("#{message}_#{Rails.application.secrets[:secret_key_base]}_#{pepper}") : nil
|
44
54
|
end
|
45
55
|
|
56
|
+
##
|
57
|
+
# 对明文 message 进行 SHA-384 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
58
|
+
# class SomeModel
|
59
|
+
# include Unidom::Common::Concerns::Sha384Digester
|
60
|
+
# def self.some_method(param_1)
|
61
|
+
# hex_digest param_1
|
62
|
+
# # 或者
|
63
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
64
|
+
# end
|
65
|
+
# end
|
46
66
|
def hex_digest(message, pepper: nil)
|
47
67
|
message.present? ? Unidom::Common::Numeration.hex(digest(message, pepper: pepper)) : nil
|
48
68
|
end
|
@@ -39,10 +39,30 @@ module Unidom::Common::Concerns::Sha512Digester
|
|
39
39
|
|
40
40
|
module ClassMethods
|
41
41
|
|
42
|
+
##
|
43
|
+
# 对明文 message 进行 SHA-512 摘要, pepper 是用于增加混乱的内容。如:
|
44
|
+
# class SomeModel
|
45
|
+
# include Unidom::Common::Concerns::Sha512Digester
|
46
|
+
# def self.some_method(param_1)
|
47
|
+
# digest param_1
|
48
|
+
# # 或者
|
49
|
+
# digest param_1, pepper: 'my_pepper'
|
50
|
+
# end
|
51
|
+
# end
|
42
52
|
def digest(message, pepper: nil)
|
43
53
|
message.present? ? Digest::SHA512.digest("#{message}_#{Rails.application.secrets[:secret_key_base]}_#{pepper}") : nil
|
44
54
|
end
|
45
55
|
|
56
|
+
##
|
57
|
+
# 对明文 message 进行 SHA-512 摘要,并以16进制的形式返回, pepper 是用于增加混乱的内容。如:
|
58
|
+
# class SomeModel
|
59
|
+
# include Unidom::Common::Concerns::Sha512Digester
|
60
|
+
# def self.some_method(param_1)
|
61
|
+
# hex_digest param_1
|
62
|
+
# # 或者
|
63
|
+
# hex_digest param_1, pepper: 'my_pepper'
|
64
|
+
# end
|
65
|
+
# end
|
46
66
|
def hex_digest(message, pepper: nil)
|
47
67
|
message.present? ? Unidom::Common::Numeration.hex(digest(message, pepper: pepper)) : nil
|
48
68
|
end
|
data/lib/unidom/common.rb
CHANGED
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.
|
4
|
+
version: '1.11'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Topbit Du
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- app/views/layouts/unidom/common/application.html.erb
|
115
115
|
- config/routes.rb
|
116
116
|
- db/migrate/20000101000000_enable_uuid_extension.rb
|
117
|
+
- db/migrate/20000101000010_enable_pgcrypto_extension.rb
|
117
118
|
- lib/tasks/common_tasks.rake
|
118
119
|
- lib/unidom/common.rb
|
119
120
|
- lib/unidom/common/data_helper.rb
|