sm-crypto-v2 0.3.12

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.
package/.babelrc ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "presets": ["es2015"]
3
+ }
package/.eslintrc.js ADDED
@@ -0,0 +1,97 @@
1
+ module.exports = {
2
+ 'extends': [
3
+ 'airbnb-base',
4
+ 'plugin:promise/recommended'
5
+ ],
6
+ 'parserOptions': {
7
+ 'ecmaVersion': 9,
8
+ 'ecmaFeatures': {
9
+ 'jsx': false
10
+ },
11
+ 'sourceType': 'module'
12
+ },
13
+ 'env': {
14
+ 'es6': true,
15
+ 'node': true,
16
+ 'jest': true
17
+ },
18
+ 'plugins': [
19
+ 'import',
20
+ 'node',
21
+ 'promise'
22
+ ],
23
+ 'rules': {
24
+ 'arrow-parens': 'off',
25
+ 'comma-dangle': [
26
+ 'error',
27
+ 'only-multiline'
28
+ ],
29
+ 'complexity': ['error', 20],
30
+ 'func-names': 'off',
31
+ 'global-require': 'off',
32
+ 'handle-callback-err': [
33
+ 'error',
34
+ '^(err|error)$'
35
+ ],
36
+ 'import/no-unresolved': [
37
+ 'error',
38
+ {
39
+ 'caseSensitive': true,
40
+ 'commonjs': true,
41
+ 'ignore': ['^[^.]']
42
+ }
43
+ ],
44
+ 'import/prefer-default-export': 'off',
45
+ 'linebreak-style': 'off',
46
+ 'no-catch-shadow': 'error',
47
+ 'no-continue': 'off',
48
+ 'no-div-regex': 'warn',
49
+ 'no-else-return': 'off',
50
+ 'no-param-reassign': 'off',
51
+ 'no-plusplus': 'off',
52
+ 'no-shadow': 'off',
53
+ 'no-multi-assign': 'off',
54
+ 'no-underscore-dangle': 'off',
55
+ 'node/no-deprecated-api': 'error',
56
+ 'node/process-exit-as-throw': 'error',
57
+ 'object-curly-spacing': [
58
+ 'error',
59
+ 'never'
60
+ ],
61
+ 'operator-linebreak': [
62
+ 'error',
63
+ 'after',
64
+ {
65
+ 'overrides': {
66
+ ':': 'before',
67
+ '?': 'before'
68
+ }
69
+ }
70
+ ],
71
+ 'prefer-arrow-callback': 'off',
72
+ 'prefer-destructuring': 'off',
73
+ 'prefer-template': 'off',
74
+ 'quote-props': [
75
+ 1,
76
+ 'as-needed',
77
+ {
78
+ 'unnecessary': true
79
+ }
80
+ ],
81
+ 'semi': [
82
+ 'error',
83
+ 'never'
84
+ ],
85
+ 'max-len': 'off',
86
+ 'no-bitwise': 'off',
87
+ 'no-mixed-operators': 'off',
88
+ },
89
+ 'globals': {
90
+ 'window': true,
91
+ 'document': true,
92
+ 'App': true,
93
+ 'Page': true,
94
+ 'Component': true,
95
+ 'Behavior': true
96
+ }
97
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,78 @@
1
+ ## 0.3.12
2
+
3
+ * 优化 sm3 运行性能
4
+
5
+ ## 0.3.11
6
+
7
+ * sm2 支持压缩公钥
8
+
9
+ ## 0.3.10
10
+
11
+ * 支持 sm3 hmac 模式
12
+
13
+
14
+ ## 0.3.9
15
+
16
+ * 补充 sm4 解密时的 padding 判断
17
+
18
+ ## 0.3.8
19
+
20
+ * sm2 解密时兼容密文可能是大写的情况
21
+
22
+ ## 0.3.7
23
+
24
+ * 默认填充改为 pkcs#7,如传入 pkcs#5 也转到 pkcs#7 逻辑
25
+
26
+ ## 0.3.6
27
+
28
+ * sm2 加解密支持二进制数据
29
+
30
+ ## 0.3.5
31
+
32
+ * sm2.generateKeyPairHex 支持完整的 BigInteger 入参
33
+
34
+ ## 0.3.4
35
+
36
+ * sm2 支持验证公钥接口
37
+ * sm2 生成密钥时支持自定义随机数
38
+
39
+ ## 0.3.3
40
+
41
+ * dist 输出改成 umd 模式
42
+
43
+ ## 0.3.2
44
+
45
+ * 修复 sm2 在 userId 长度大于 31 时新旧版本验签不通过的问题
46
+ ## 0.3.0
47
+
48
+ * sm2、sm3 重构
49
+ * sm4 支持 cbc 模式
50
+
51
+ ## 0.2.7
52
+
53
+ * 优化 sm3 性能
54
+
55
+ ## 0.2.5
56
+
57
+ * sm3 支持字节数组输入
58
+
59
+ ## 0.2.4
60
+
61
+ * 修复 sm4 四字节字符输出编码
62
+
63
+ ## 0.2.3
64
+
65
+ * sm3/sm4 支持输入四字节字符
66
+
67
+ ## 0.2.2
68
+
69
+ * sm3 支持中文输入
70
+
71
+ ## 0.2.1
72
+
73
+ * 修复 sm2 点 16 进制串可能不满 64 位的问题
74
+
75
+ ## 0.2.0
76
+
77
+ * sm4 默认支持 pkcs#5 填充方式
78
+ * sm4 支持输入输出为字符串
package/LICENCE_MIT ADDED
@@ -0,0 +1,7 @@
1
+ Copyright © 2018 june01
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # sm-crypto
2
+
3
+ 国密算法 sm2、sm3 和 sm4 的 TypeScript 实现。参数支持 TypedArray,导出 esm/cjs。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install --save sm-crypto-v2
9
+ ```
10
+
11
+ ## sm2
12
+
13
+ ### 获取密钥对
14
+
15
+ ```js
16
+ import { sm2 } from 'sm-crypto-v2'
17
+
18
+ let keypair = sm2.generateKeyPairHex()
19
+
20
+ publicKey = keypair.publicKey // 公钥
21
+ privateKey = keypair.privateKey // 私钥
22
+
23
+ // 默认生成公钥 130 位太长,可以压缩公钥到 66 位
24
+ const compressedPublicKey = sm2.compressPublicKeyHex(publicKey) // compressedPublicKey 和 publicKey 等价
25
+ sm2.comparePublicKeyHex(publicKey, compressedPublicKey) // 判断公钥是否等价
26
+
27
+ // 自定义随机数,参数会直接透传给 jsbn 库的 BigInteger 构造器
28
+ // 注意:开发者使用自定义随机数,需要自行确保传入的随机数符合密码学安全
29
+ let keypair2 = sm2.generateKeyPairHex('123123123123123')
30
+ let keypair3 = sm2.generateKeyPairHex(256, SecureRandom)
31
+
32
+ let verifyResult = sm2.verifyPublicKey(publicKey) // 验证公钥
33
+ verifyResult = sm2.verifyPublicKey(compressedPublicKey) // 验证公钥
34
+ ```
35
+
36
+ ### 加密解密
37
+
38
+ ```js
39
+ import { sm2 } from 'sm-crypto-v2'
40
+ const cipherMode = 1 // 1 - C1C3C2,0 - C1C2C3,默认为1
41
+
42
+ let encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode) // 加密结果
43
+ let decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode) // 解密结果
44
+
45
+ encryptData = sm2.doEncrypt(msgArray, publicKey, cipherMode) // 加密结果,输入数组
46
+ decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode, {output: 'array'}) // 解密结果,输出数组
47
+ ```
48
+
49
+ ### 签名验签
50
+
51
+ > ps:理论上来说,只做纯签名是最快的。
52
+
53
+ ```js
54
+ import { sm2 } from 'sm-crypto-v2'
55
+ // 纯签名 + 生成椭圆曲线点
56
+ let sigValueHex = sm2.doSignature(msg, privateKey) // 签名
57
+ let verifyResult = sm2.doVerifySignature(msg, sigValueHex, publicKey) // 验签结果
58
+
59
+ // 纯签名
60
+ let sigValueHex2 = sm2.doSignature(msg, privateKey, {
61
+ pointPool: [sm2.getPoint(), sm2.getPoint(), sm2.getPoint(), sm2.getPoint()], // 传入事先已生成好的椭圆曲线点,可加快签名速度
62
+ }) // 签名
63
+ let verifyResult2 = sm2.doVerifySignature(msg, sigValueHex2, publicKey) // 验签结果
64
+
65
+ // 纯签名 + 生成椭圆曲线点 + der编解码
66
+ let sigValueHex3 = sm2.doSignature(msg, privateKey, {
67
+ der: true,
68
+ }) // 签名
69
+ let verifyResult3 = sm2.doVerifySignature(msg, sigValueHex3, publicKey, {
70
+ der: true,
71
+ }) // 验签结果
72
+
73
+ // 纯签名 + 生成椭圆曲线点 + sm3杂凑
74
+ let sigValueHex4 = sm2.doSignature(msg, privateKey, {
75
+ hash: true,
76
+ }) // 签名
77
+ let verifyResult4 = sm2.doVerifySignature(msg, sigValueHex4, publicKey, {
78
+ hash: true,
79
+ }) // 验签结果
80
+
81
+ // 纯签名 + 生成椭圆曲线点 + sm3杂凑(不做公钥推导)
82
+ let sigValueHex5 = sm2.doSignature(msg, privateKey, {
83
+ hash: true,
84
+ publicKey, // 传入公钥的话,可以去掉sm3杂凑中推导公钥的过程,速度会比纯签名 + 生成椭圆曲线点 + sm3杂凑快
85
+ })
86
+ let verifyResult5 = sm2.doVerifySignature(msg, sigValueHex5, publicKey, {
87
+ hash: true,
88
+ publicKey,
89
+ })
90
+
91
+ // 纯签名 + 生成椭圆曲线点 + sm3杂凑 + 不做公钥推 + 添加 userId(长度小于 8192)
92
+ // 默认 userId 值为 1234567812345678
93
+ let sigValueHex6 = sm2.doSignature(msgString, privateKey, {
94
+ hash: true,
95
+ publicKey,
96
+ userId: 'testUserId',
97
+ })
98
+ let verifyResult6 = sm2.doVerifySignature(msgString, sigValueHex6, publicKey, {
99
+ hash: true,
100
+ userId: 'testUserId',
101
+ })
102
+ ```
103
+
104
+ ### 获取椭圆曲线点
105
+
106
+ ```js
107
+ import { sm2 } from 'sm-crypto-v2'
108
+ let point = sm2.getPoint() // 获取一个椭圆曲线点,可在sm2签名时传入
109
+ ```
110
+
111
+ ## sm3
112
+
113
+ ```js
114
+ import { sm3 } from 'sm-crypto-v2'
115
+ let hashData = sm3('abc') // 杂凑
116
+
117
+ // hmac
118
+ hashData = sm3('abc', {
119
+ key: 'daac25c1512fe50f79b0e4526b93f5c0e1460cef40b6dd44af13caec62e8c60e0d885f3c6d6fb51e530889e6fd4ac743a6d332e68a0f2a3923f42585dceb93e9', // 要求为 16 进制串或字节数组
120
+ })
121
+ ```
122
+
123
+ ## sm4
124
+
125
+ ### 加密
126
+
127
+ ```js
128
+ import { sm4 } from 'sm-crypto-v2'
129
+ const msg = 'hello world! 我是 juneandgreen.' // 可以为 utf8 串或字节数组
130
+ const key = '0123456789abcdeffedcba9876543210' // 可以为 16 进制串或字节数组,要求为 128 比特
131
+
132
+ let encryptData = sm4.encrypt(msg, key) // 加密,默认输出 16 进制字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充)
133
+ let encryptData = sm4.encrypt(msg, key, {padding: 'none'}) // 加密,不使用 padding
134
+ let encryptData = sm4.encrypt(msg, key, {padding: 'none', output: 'array'}) // 加密,不使用 padding,输出为字节数组
135
+ let encryptData = sm4.encrypt(msg, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 加密,cbc 模式
136
+ ```
137
+
138
+ ### 解密
139
+
140
+ ```js
141
+ import { sm4 } from 'sm-crypto-v2'
142
+ const encryptData = '0e395deb10f6e8a17e17823e1fd9bd98a1bff1df508b5b8a1efb79ec633d1bb129432ac1b74972dbe97bab04f024e89c' // 可以为 16 进制串或字节数组
143
+ const key = '0123456789abcdeffedcba9876543210' // 可以为 16 进制串或字节数组,要求为 128 比特
144
+
145
+ let decryptData = sm4.decrypt(encryptData, key) // 解密,默认输出 utf8 字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充)
146
+ let decryptData = sm4.decrypt(encryptData, key, {padding: 'none'}) // 解密,不使用 padding
147
+ let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array'}) // 解密,不使用 padding,输出为字节数组
148
+ let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密,cbc 模式
149
+ ```
150
+
151
+ ## 其他实现
152
+
153
+ * 原 js 版本:[https://github.com/JuneAndGreen/sm-crypto](https://github.com/JuneAndGreen/sm-crypto)
154
+ * 小程序移植版:[https://github.com/wechat-miniprogram/sm-crypto](https://github.com/wechat-miniprogram/sm-crypto)
155
+ * java 实现(感谢 @antherd 提供):[https://github.com/antherd/sm-crypto](https://github.com/antherd/sm-crypto)
156
+ * dart 实现(感谢 @luckykellan 提供):[https://github.com/luckykellan/dart_sm](https://github.com/luckykellan/dart_sm)
157
+
158
+ ## 协议
159
+
160
+ MIT