@ivujs/i-utils 1.1.1 → 1.1.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.
- package/README.md +17 -19
- package/dist/cjs/id/index.cjs +26 -43
- package/dist/cjs/index.cjs +8 -398
- package/dist/es/id/index.d.ts +16 -0
- package/dist/es/id/index.mjs +28 -42
- package/dist/es/index.d.ts +2 -0
- package/dist/es/index.mjs +8 -58
- package/dist/index.d.ts +21 -0
- package/dist/lib/index.full.cjs.js +38 -0
- package/dist/lib/index.full.cjs.min.js +2 -0
- package/dist/lib/index.full.cjs.min.js.map +1 -0
- package/dist/lib/index.full.esm.js +35 -15822
- package/dist/lib/index.full.esm.min.js +2 -58
- package/dist/lib/index.full.esm.min.js.map +1 -0
- package/dist/lib/index.full.umd.js +39 -16176
- package/dist/lib/index.full.umd.min.js +2 -0
- package/dist/lib/index.full.umd.min.js.map +1 -0
- package/package.json +77 -54
- package/dist/cjs/ID-card/index.cjs +0 -122
- package/dist/cjs/array/index.cjs +0 -465
- package/dist/cjs/clipboard/index.cjs +0 -135
- package/dist/cjs/color/index.cjs +0 -210
- package/dist/cjs/constants/date.cjs +0 -186
- package/dist/cjs/constants/lang.cjs +0 -13
- package/dist/cjs/constants/math.cjs +0 -13
- package/dist/cjs/constants/regexp.cjs +0 -57
- package/dist/cjs/constants/sort.cjs +0 -15
- package/dist/cjs/cookie/index.cjs +0 -71
- package/dist/cjs/crypto/aes/aes.cjs +0 -533
- package/dist/cjs/crypto/aes/index.cjs +0 -28
- package/dist/cjs/crypto/base32/base32.cjs +0 -385
- package/dist/cjs/crypto/base32/index.cjs +0 -44
- package/dist/cjs/crypto/base64/base64.cjs +0 -433
- package/dist/cjs/crypto/base64/index.cjs +0 -64
- package/dist/cjs/crypto/des/des.cjs +0 -1054
- package/dist/cjs/crypto/des/index.cjs +0 -30
- package/dist/cjs/crypto/md5/index.cjs +0 -25
- package/dist/cjs/crypto/md5/md5.cjs +0 -944
- package/dist/cjs/crypto/sha/sha1/index.cjs +0 -25
- package/dist/cjs/crypto/sha/sha1/sha1.cjs +0 -602
- package/dist/cjs/crypto/sha/sha256/index.cjs +0 -46
- package/dist/cjs/crypto/sha/sha256/sha256.cjs +0 -654
- package/dist/cjs/crypto/sha/sha3/index.cjs +0 -44
- package/dist/cjs/crypto/sha/sha3/sha3.cjs +0 -747
- package/dist/cjs/crypto/sha/sha512/index.cjs +0 -88
- package/dist/cjs/crypto/sha/sha512/sha512.cjs +0 -1186
- package/dist/cjs/crypto/sm/lib/asn1.cjs +0 -167
- package/dist/cjs/crypto/sm/lib/ec.cjs +0 -352
- package/dist/cjs/crypto/sm/lib/jsbn.cjs +0 -1633
- package/dist/cjs/crypto/sm/lib/sm3.cjs +0 -173
- package/dist/cjs/crypto/sm/lib/utils.cjs +0 -198
- package/dist/cjs/crypto/sm/sm2/index.cjs +0 -122
- package/dist/cjs/crypto/sm/sm2/sm2.cjs +0 -269
- package/dist/cjs/crypto/sm/sm3/index.cjs +0 -15
- package/dist/cjs/crypto/sm/sm3/sm3.cjs +0 -103
- package/dist/cjs/crypto/sm/sm4/index.cjs +0 -28
- package/dist/cjs/crypto/sm/sm4/sm4.cjs +0 -362
- package/dist/cjs/crypto/tea/index.cjs +0 -26
- package/dist/cjs/crypto/tea/tea.cjs +0 -215
- package/dist/cjs/date/index.cjs +0 -1349
- package/dist/cjs/desensitized/index.cjs +0 -74
- package/dist/cjs/device/index.cjs +0 -159
- package/dist/cjs/dom/index.cjs +0 -176
- package/dist/cjs/file/index.cjs +0 -339
- package/dist/cjs/function/index.cjs +0 -67
- package/dist/cjs/keycode/index.cjs +0 -142
- package/dist/cjs/math/index.cjs +0 -284
- package/dist/cjs/number/index.cjs +0 -14
- package/dist/cjs/object/index.cjs +0 -216
- package/dist/cjs/pagination/index.cjs +0 -140
- package/dist/cjs/random/index.cjs +0 -25
- package/dist/cjs/regexp/index.cjs +0 -109
- package/dist/cjs/storage/index.cjs +0 -11
- package/dist/cjs/storage/localStorage.cjs +0 -40
- package/dist/cjs/storage/sessionStorage.cjs +0 -40
- package/dist/cjs/string/index.cjs +0 -338
- package/dist/cjs/url/index.cjs +0 -279
- package/dist/cjs/validate/index.cjs +0 -487
- package/dist/cjs/weapp/index.cjs +0 -151
- package/dist/es/ID-card/index.mjs +0 -116
- package/dist/es/array/index.mjs +0 -437
- package/dist/es/clipboard/index.mjs +0 -129
- package/dist/es/color/index.mjs +0 -200
- package/dist/es/constants/date.mjs +0 -184
- package/dist/es/constants/lang.mjs +0 -11
- package/dist/es/constants/math.mjs +0 -11
- package/dist/es/constants/regexp.mjs +0 -55
- package/dist/es/constants/sort.mjs +0 -13
- package/dist/es/cookie/index.mjs +0 -65
- package/dist/es/crypto/aes/aes.mjs +0 -529
- package/dist/es/crypto/aes/index.mjs +0 -25
- package/dist/es/crypto/base32/base32.mjs +0 -381
- package/dist/es/crypto/base32/index.mjs +0 -39
- package/dist/es/crypto/base64/base64.mjs +0 -427
- package/dist/es/crypto/base64/index.mjs +0 -57
- package/dist/es/crypto/des/des.mjs +0 -1050
- package/dist/es/crypto/des/index.mjs +0 -27
- package/dist/es/crypto/md5/index.mjs +0 -22
- package/dist/es/crypto/md5/md5.mjs +0 -941
- package/dist/es/crypto/sha/sha1/index.mjs +0 -22
- package/dist/es/crypto/sha/sha1/sha1.mjs +0 -599
- package/dist/es/crypto/sha/sha256/index.mjs +0 -41
- package/dist/es/crypto/sha/sha256/sha256.mjs +0 -649
- package/dist/es/crypto/sha/sha3/index.mjs +0 -39
- package/dist/es/crypto/sha/sha3/sha3.mjs +0 -742
- package/dist/es/crypto/sha/sha512/index.mjs +0 -79
- package/dist/es/crypto/sha/sha512/sha512.mjs +0 -1177
- package/dist/es/crypto/sm/lib/asn1.mjs +0 -164
- package/dist/es/crypto/sm/lib/ec.mjs +0 -349
- package/dist/es/crypto/sm/lib/jsbn.mjs +0 -1630
- package/dist/es/crypto/sm/lib/sm3.mjs +0 -170
- package/dist/es/crypto/sm/lib/utils.mjs +0 -186
- package/dist/es/crypto/sm/sm2/index.mjs +0 -111
- package/dist/es/crypto/sm/sm2/sm2.mjs +0 -258
- package/dist/es/crypto/sm/sm3/index.mjs +0 -13
- package/dist/es/crypto/sm/sm3/sm3.mjs +0 -99
- package/dist/es/crypto/sm/sm4/index.mjs +0 -25
- package/dist/es/crypto/sm/sm4/sm4.mjs +0 -359
- package/dist/es/crypto/tea/index.mjs +0 -23
- package/dist/es/crypto/tea/tea.mjs +0 -211
- package/dist/es/date/index.mjs +0 -1262
- package/dist/es/desensitized/index.mjs +0 -69
- package/dist/es/device/index.mjs +0 -145
- package/dist/es/dom/index.mjs +0 -164
- package/dist/es/file/index.mjs +0 -323
- package/dist/es/function/index.mjs +0 -63
- package/dist/es/keycode/index.mjs +0 -139
- package/dist/es/math/index.mjs +0 -274
- package/dist/es/number/index.mjs +0 -12
- package/dist/es/object/index.mjs +0 -203
- package/dist/es/pagination/index.mjs +0 -134
- package/dist/es/random/index.mjs +0 -22
- package/dist/es/regexp/index.mjs +0 -98
- package/dist/es/storage/index.mjs +0 -9
- package/dist/es/storage/localStorage.mjs +0 -35
- package/dist/es/storage/sessionStorage.mjs +0 -35
- package/dist/es/string/index.mjs +0 -321
- package/dist/es/url/index.mjs +0 -261
- package/dist/es/validate/index.mjs +0 -451
- package/dist/es/weapp/index.mjs +0 -140
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
// 消息扩展
|
|
2
|
-
const W = new Uint32Array(68);
|
|
3
|
-
const M = new Uint32Array(64); // W'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 循环左移
|
|
7
|
-
*/
|
|
8
|
-
function rotl(x, n) {
|
|
9
|
-
const s = n & 31;
|
|
10
|
-
return (x << s) | (x >>> (32 - s));
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 二进制异或运算
|
|
15
|
-
*/
|
|
16
|
-
function xor(x, y) {
|
|
17
|
-
const result = [];
|
|
18
|
-
for (let i = x.length - 1; i >= 0; i--) result[i] = (x[i] ^ y[i]) & 0xff;
|
|
19
|
-
return result;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 压缩函数中的置换函数 P0(X) = X xor (X <<< 9) xor (X <<< 17)
|
|
24
|
-
*/
|
|
25
|
-
function P0(X) {
|
|
26
|
-
return X ^ rotl(X, 9) ^ rotl(X, 17);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 消息扩展中的置换函数 P1(X) = X xor (X <<< 15) xor (X <<< 23)
|
|
31
|
-
*/
|
|
32
|
-
function P1(X) {
|
|
33
|
-
return X ^ rotl(X, 15) ^ rotl(X, 23);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* sm3 本体
|
|
38
|
-
*/
|
|
39
|
-
function sm3(array) {
|
|
40
|
-
let len = array.length * 8;
|
|
41
|
-
|
|
42
|
-
// k 是满足 len + 1 + k = 448mod512 的最小的非负整数
|
|
43
|
-
let k = len % 512;
|
|
44
|
-
// 如果 448 <= (512 % len) < 512,需要多补充 (len % 448) 比特'0'以满足总比特长度为512的倍数
|
|
45
|
-
k = k >= 448 ? 512 - (k % 448) - 1 : 448 - k - 1;
|
|
46
|
-
|
|
47
|
-
// 填充
|
|
48
|
-
const kArr = new Array((k - 7) / 8);
|
|
49
|
-
const lenArr = new Array(8);
|
|
50
|
-
for (let i = 0, len = kArr.length; i < len; i++) kArr[i] = 0;
|
|
51
|
-
for (let i = 0, len = lenArr.length; i < len; i++) lenArr[i] = 0;
|
|
52
|
-
len = len.toString(2);
|
|
53
|
-
for (let i = 7; i >= 0; i--) {
|
|
54
|
-
if (len.length > 8) {
|
|
55
|
-
const start = len.length - 8;
|
|
56
|
-
lenArr[i] = parseInt(len.substr(start), 2);
|
|
57
|
-
len = len.substr(0, start);
|
|
58
|
-
} else if (len.length > 0) {
|
|
59
|
-
lenArr[i] = parseInt(len, 2);
|
|
60
|
-
len = "";
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
const m = new Uint8Array([...array, 0x80, ...kArr, ...lenArr]);
|
|
64
|
-
const dataView = new DataView(m.buffer, 0);
|
|
65
|
-
|
|
66
|
-
// 迭代压缩
|
|
67
|
-
const n = m.length / 64;
|
|
68
|
-
const V = new Uint32Array([
|
|
69
|
-
0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600, 0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e,
|
|
70
|
-
]);
|
|
71
|
-
for (let i = 0; i < n; i++) {
|
|
72
|
-
W.fill(0);
|
|
73
|
-
M.fill(0);
|
|
74
|
-
|
|
75
|
-
// 将消息分组B划分为 16 个字 W0, W1,……,W15
|
|
76
|
-
const start = 16 * i;
|
|
77
|
-
for (let j = 0; j < 16; j++) {
|
|
78
|
-
W[j] = dataView.getUint32((start + j) * 4, false);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// W16 ~ W67:W[j] <- P1(W[j−16] xor W[j−9] xor (W[j−3] <<< 15)) xor (W[j−13] <<< 7) xor W[j−6]
|
|
82
|
-
for (let j = 16; j < 68; j++) {
|
|
83
|
-
W[j] = P1(W[j - 16] ^ W[j - 9] ^ rotl(W[j - 3], 15)) ^ rotl(W[j - 13], 7) ^ W[j - 6];
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// W′0 ~ W′63:W′[j] = W[j] xor W[j+4]
|
|
87
|
-
for (let j = 0; j < 64; j++) {
|
|
88
|
-
M[j] = W[j] ^ W[j + 4];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// 压缩
|
|
92
|
-
const T1 = 0x79cc4519;
|
|
93
|
-
const T2 = 0x7a879d8a;
|
|
94
|
-
// 字寄存器
|
|
95
|
-
let A = V[0];
|
|
96
|
-
let B = V[1];
|
|
97
|
-
let C = V[2];
|
|
98
|
-
let D = V[3];
|
|
99
|
-
let E = V[4];
|
|
100
|
-
let F = V[5];
|
|
101
|
-
let G = V[6];
|
|
102
|
-
let H = V[7];
|
|
103
|
-
// 中间变量
|
|
104
|
-
let SS1;
|
|
105
|
-
let SS2;
|
|
106
|
-
let TT1;
|
|
107
|
-
let TT2;
|
|
108
|
-
let T;
|
|
109
|
-
for (let j = 0; j < 64; j++) {
|
|
110
|
-
T = j >= 0 && j <= 15 ? T1 : T2;
|
|
111
|
-
SS1 = rotl(rotl(A, 12) + E + rotl(T, j), 7);
|
|
112
|
-
SS2 = SS1 ^ rotl(A, 12);
|
|
113
|
-
|
|
114
|
-
TT1 = (j >= 0 && j <= 15 ? A ^ B ^ C : (A & B) | (A & C) | (B & C)) + D + SS2 + M[j];
|
|
115
|
-
TT2 = (j >= 0 && j <= 15 ? E ^ F ^ G : (E & F) | (~E & G)) + H + SS1 + W[j];
|
|
116
|
-
|
|
117
|
-
D = C;
|
|
118
|
-
C = rotl(B, 9);
|
|
119
|
-
B = A;
|
|
120
|
-
A = TT1;
|
|
121
|
-
H = G;
|
|
122
|
-
G = rotl(F, 19);
|
|
123
|
-
F = E;
|
|
124
|
-
E = P0(TT2);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
V[0] ^= A;
|
|
128
|
-
V[1] ^= B;
|
|
129
|
-
V[2] ^= C;
|
|
130
|
-
V[3] ^= D;
|
|
131
|
-
V[4] ^= E;
|
|
132
|
-
V[5] ^= F;
|
|
133
|
-
V[6] ^= G;
|
|
134
|
-
V[7] ^= H;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// 转回 uint8
|
|
138
|
-
const result = [];
|
|
139
|
-
for (let i = 0, len = V.length; i < len; i++) {
|
|
140
|
-
const word = V[i];
|
|
141
|
-
result.push((word & 0xff000000) >>> 24, (word & 0xff0000) >>> 16, (word & 0xff00) >>> 8, word & 0xff);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return result;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* hmac 实现
|
|
149
|
-
*/
|
|
150
|
-
const blockLen = 64;
|
|
151
|
-
const iPad = new Uint8Array(blockLen);
|
|
152
|
-
const oPad = new Uint8Array(blockLen);
|
|
153
|
-
for (let i = 0; i < blockLen; i++) {
|
|
154
|
-
iPad[i] = 0x36;
|
|
155
|
-
oPad[i] = 0x5c;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function hmac(input, key) {
|
|
159
|
-
// 密钥填充
|
|
160
|
-
if (key.length > blockLen) key = sm3(key);
|
|
161
|
-
while (key.length < blockLen) key.push(0);
|
|
162
|
-
|
|
163
|
-
const iPadKey = xor(key, iPad);
|
|
164
|
-
const oPadKey = xor(key, oPad);
|
|
165
|
-
|
|
166
|
-
const hash = sm3([...iPadKey, ...input]);
|
|
167
|
-
return sm3([...oPadKey, ...hash]);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export { hmac, sm3 };
|
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import * as jsbn from './jsbn.mjs';
|
|
2
|
-
import * as ec from './ec.mjs';
|
|
3
|
-
|
|
4
|
-
const { BigInteger, SecureRandom } = jsbn;
|
|
5
|
-
const { ECCurveFp } = ec;
|
|
6
|
-
|
|
7
|
-
const rng = new SecureRandom();
|
|
8
|
-
const { curve, G, n } = generateEcparam();
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 获取公共椭圆曲线
|
|
12
|
-
*/
|
|
13
|
-
function getGlobalCurve() {
|
|
14
|
-
return curve;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 生成ecparam
|
|
19
|
-
*/
|
|
20
|
-
function generateEcparam() {
|
|
21
|
-
// 椭圆曲线
|
|
22
|
-
const p = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16);
|
|
23
|
-
const a = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16);
|
|
24
|
-
const b = new BigInteger("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16);
|
|
25
|
-
const curve = new ECCurveFp(p, a, b);
|
|
26
|
-
|
|
27
|
-
// 基点
|
|
28
|
-
const gxHex = "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7";
|
|
29
|
-
const gyHex = "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0";
|
|
30
|
-
const G = curve.decodePointHex("04" + gxHex + gyHex);
|
|
31
|
-
|
|
32
|
-
const n = new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16);
|
|
33
|
-
|
|
34
|
-
return { curve, G, n };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 生成密钥对:publicKey = privateKey * G
|
|
39
|
-
*/
|
|
40
|
-
function generateKeyPairHex(a, b, c) {
|
|
41
|
-
const random = a ? new BigInteger(a, b, c) : new BigInteger(n.bitLength(), rng);
|
|
42
|
-
const d = random.mod(n.subtract(BigInteger.ONE)).add(BigInteger.ONE); // 随机数
|
|
43
|
-
const privateKey = leftPad(d.toString(16), 64);
|
|
44
|
-
|
|
45
|
-
const P = G.multiply(d); // P = dG,p 为公钥,d 为私钥
|
|
46
|
-
const Px = leftPad(P.getX().toBigInteger().toString(16), 64);
|
|
47
|
-
const Py = leftPad(P.getY().toBigInteger().toString(16), 64);
|
|
48
|
-
const publicKey = "04" + Px + Py;
|
|
49
|
-
|
|
50
|
-
return { privateKey, publicKey };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* 生成压缩公钥
|
|
55
|
-
*/
|
|
56
|
-
function compressPublicKeyHex(s) {
|
|
57
|
-
if (s.length !== 130) throw new Error("Invalid public key to compress");
|
|
58
|
-
|
|
59
|
-
const len = (s.length - 2) / 2;
|
|
60
|
-
const xHex = s.substr(2, len);
|
|
61
|
-
const y = new BigInteger(s.substr(len + 2, len), 16);
|
|
62
|
-
|
|
63
|
-
let prefix = "03";
|
|
64
|
-
if (y.mod(new BigInteger("2")).equals(BigInteger.ZERO)) prefix = "02";
|
|
65
|
-
|
|
66
|
-
return prefix + xHex;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* utf8串转16进制串
|
|
71
|
-
*/
|
|
72
|
-
function utf8ToHex(input) {
|
|
73
|
-
input = unescape(encodeURIComponent(input));
|
|
74
|
-
|
|
75
|
-
const length = input.length;
|
|
76
|
-
|
|
77
|
-
// 转换到字数组
|
|
78
|
-
const words = [];
|
|
79
|
-
for (let i = 0; i < length; i++) {
|
|
80
|
-
words[i >>> 2] |= (input.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// 转换到16进制
|
|
84
|
-
const hexChars = [];
|
|
85
|
-
for (let i = 0; i < length; i++) {
|
|
86
|
-
const bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
87
|
-
hexChars.push((bite >>> 4).toString(16));
|
|
88
|
-
hexChars.push((bite & 0x0f).toString(16));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return hexChars.join("");
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 补全16进制字符串
|
|
96
|
-
*/
|
|
97
|
-
function leftPad(input, num) {
|
|
98
|
-
if (input.length >= num) return input;
|
|
99
|
-
|
|
100
|
-
return new Array(num - input.length + 1).join("0") + input;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 转成16进制串
|
|
105
|
-
*/
|
|
106
|
-
function arrayToHex(arr) {
|
|
107
|
-
return arr
|
|
108
|
-
.map((item) => {
|
|
109
|
-
item = item.toString(16);
|
|
110
|
-
return item.length === 1 ? "0" + item : item;
|
|
111
|
-
})
|
|
112
|
-
.join("");
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* 转成utf8串
|
|
117
|
-
*/
|
|
118
|
-
function arrayToUtf8(arr) {
|
|
119
|
-
const words = [];
|
|
120
|
-
let j = 0;
|
|
121
|
-
for (let i = 0; i < arr.length * 2; i += 2) {
|
|
122
|
-
words[i >>> 3] |= parseInt(arr[j], 10) << (24 - (i % 8) * 4);
|
|
123
|
-
j++;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
try {
|
|
127
|
-
const latin1Chars = [];
|
|
128
|
-
|
|
129
|
-
for (let i = 0; i < arr.length; i++) {
|
|
130
|
-
const bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
131
|
-
latin1Chars.push(String.fromCharCode(bite));
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return decodeURIComponent(escape(latin1Chars.join("")));
|
|
135
|
-
} catch (e) {
|
|
136
|
-
throw new Error("Malformed UTF-8 data");
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 转成字节数组
|
|
142
|
-
*/
|
|
143
|
-
function hexToArray(hexStr) {
|
|
144
|
-
const words = [];
|
|
145
|
-
let hexStrLength = hexStr.length;
|
|
146
|
-
|
|
147
|
-
if (hexStrLength % 2 !== 0) {
|
|
148
|
-
hexStr = leftPad(hexStr, hexStrLength + 1);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
hexStrLength = hexStr.length;
|
|
152
|
-
|
|
153
|
-
for (let i = 0; i < hexStrLength; i += 2) {
|
|
154
|
-
words.push(parseInt(hexStr.substr(i, 2), 16));
|
|
155
|
-
}
|
|
156
|
-
return words;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* 验证公钥是否为椭圆曲线上的点
|
|
161
|
-
*/
|
|
162
|
-
function verifyPublicKey(publicKey) {
|
|
163
|
-
const point = curve.decodePointHex(publicKey);
|
|
164
|
-
if (!point) return false;
|
|
165
|
-
|
|
166
|
-
const x = point.getX();
|
|
167
|
-
const y = point.getY();
|
|
168
|
-
|
|
169
|
-
// 验证 y^2 是否等于 x^3 + ax + b
|
|
170
|
-
return y.square().equals(x.multiply(x.square()).add(x.multiply(curve.a)).add(curve.b));
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* 验证公钥是否等价,等价返回true
|
|
175
|
-
*/
|
|
176
|
-
function comparePublicKeyHex(publicKey1, publicKey2) {
|
|
177
|
-
const point1 = curve.decodePointHex(publicKey1);
|
|
178
|
-
if (!point1) return false;
|
|
179
|
-
|
|
180
|
-
const point2 = curve.decodePointHex(publicKey2);
|
|
181
|
-
if (!point2) return false;
|
|
182
|
-
|
|
183
|
-
return point1.equals(point2);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
export { arrayToHex, arrayToUtf8, comparePublicKeyHex, compressPublicKeyHex, generateEcparam, generateKeyPairHex, getGlobalCurve, hexToArray, leftPad, utf8ToHex, verifyPublicKey };
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { generateKeyPairHex as generateKeyPairHex$1, compressPublicKeyHex as compressPublicKeyHex$1, comparePublicKeyHex as comparePublicKeyHex$1, verifyPublicKey as verifyPublicKey$1, doEncrypt, doDecrypt, doSignature as doSignature$1, doVerifySignature as doVerifySignature$1, getPoint as getPoint$1, getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey$1 } from './sm2.mjs';
|
|
2
|
-
|
|
3
|
-
// 获取密钥对
|
|
4
|
-
/**
|
|
5
|
-
* sm2 生成公钥和私钥
|
|
6
|
-
* @param {String} str 字符串
|
|
7
|
-
* @param {Number} rnd 随机数
|
|
8
|
-
* @returns {Object} 返回公钥和私钥
|
|
9
|
-
*/
|
|
10
|
-
function generateKeyPairHex(str, rnd) {
|
|
11
|
-
return generateKeyPairHex$1(str, rnd);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* sm2 压缩公钥
|
|
16
|
-
* @param {String} publicKey 公钥
|
|
17
|
-
* @returns {String} 返回公钥字符串
|
|
18
|
-
*/
|
|
19
|
-
function compressPublicKeyHex(publicKey) {
|
|
20
|
-
return compressPublicKeyHex$1(publicKey);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* sm2 对比公钥是否等价
|
|
25
|
-
* @param {String} publicKey 公钥
|
|
26
|
-
* @param {String} compressedPublicKey 压缩后的公钥
|
|
27
|
-
* @returns {Boolean} 返回结果
|
|
28
|
-
*/
|
|
29
|
-
function comparePublicKeyHex(publicKey, compressedPublicKey) {
|
|
30
|
-
return comparePublicKeyHex$1(publicKey, compressedPublicKey);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 验证密钥对
|
|
34
|
-
/**
|
|
35
|
-
* sm2 验证公钥
|
|
36
|
-
* @param {String} publicKey 公钥,也可以传压缩后的公钥
|
|
37
|
-
* @returns {Boolean} 返回结果
|
|
38
|
-
*/
|
|
39
|
-
function verifyPublicKey(publicKey) {
|
|
40
|
-
return verifyPublicKey$1(publicKey);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// 加密解密
|
|
44
|
-
/**
|
|
45
|
-
* sm2 加密
|
|
46
|
-
* @param {String} str 字符串
|
|
47
|
-
* @param {String} publicKey 公钥
|
|
48
|
-
* @param {Number} cipherMode 加密模式,1(C1C3C2)和0(C1C2C3),默认1
|
|
49
|
-
* @returns {String} 返回加密后的字符串
|
|
50
|
-
*/
|
|
51
|
-
function encrypt(str, publicKey, cipherMode = 1) {
|
|
52
|
-
return doEncrypt(str, publicKey, cipherMode);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* sm2 解密
|
|
57
|
-
* @param {String} str 字符串
|
|
58
|
-
* @param {String} privateKey 私钥
|
|
59
|
-
* @param {Number} cipherMode 解密模式,1(C1C3C2)和0(C1C2C3),默认1
|
|
60
|
-
* @param {Object} outputConfig 输出结果配置,{output:'string/array'}
|
|
61
|
-
* @returns {String,Array} 返回解密后的数据
|
|
62
|
-
*/
|
|
63
|
-
function decrypt(str, privateKey, cipherMode = 1, outputConfig = { output: "string" }) {
|
|
64
|
-
return doDecrypt(str, privateKey, cipherMode, outputConfig);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// 签名验签
|
|
68
|
-
/**
|
|
69
|
-
* sm2 生成签名
|
|
70
|
-
* @param {String} str 字符串
|
|
71
|
-
* @param {String} privateKey 私钥
|
|
72
|
-
* @param {Object} signatureConfig 签名配置
|
|
73
|
-
* @returns {String} 返回签名信息
|
|
74
|
-
*/
|
|
75
|
-
function doSignature(str, privateKey, signatureConfig) {
|
|
76
|
-
return doSignature$1(str, privateKey, signatureConfig);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* sm2 验证签名
|
|
81
|
-
* @param {String} str 字符串
|
|
82
|
-
* @param {String} signHex 生成签名的信息
|
|
83
|
-
* @param {String} publicKey 公钥
|
|
84
|
-
* @param {Object} verifyConfig 验证配置
|
|
85
|
-
* @returns {String} 返回结果
|
|
86
|
-
*/
|
|
87
|
-
function doVerifySignature(str, signHex, publicKey, verifyConfig) {
|
|
88
|
-
return doVerifySignature$1(str, signHex, publicKey, verifyConfig);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// 获取椭圆曲线点
|
|
92
|
-
/**
|
|
93
|
-
* sm2 获取椭圆曲线点
|
|
94
|
-
* @returns {Object} 返回椭圆曲线点,可在sm2签名时传入
|
|
95
|
-
*/
|
|
96
|
-
function getPoint() {
|
|
97
|
-
return getPoint$1();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// 根据私钥获取公钥
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* sm2 根据私钥获取公钥
|
|
104
|
-
* @param {String} privateKey 私钥
|
|
105
|
-
* @returns {String} 返回公钥
|
|
106
|
-
*/
|
|
107
|
-
function getPublicKeyFromPrivateKey(privateKey) {
|
|
108
|
-
return getPublicKeyFromPrivateKey$1(privateKey);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export { comparePublicKeyHex, compressPublicKeyHex, decrypt, doSignature, doVerifySignature, encrypt, generateKeyPairHex, getPoint, getPublicKeyFromPrivateKey, verifyPublicKey };
|