sm-crypto-v2 1.10.0 → 1.10.1
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/CHANGELOG.md +8 -0
- package/README.md +16 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -4
- package/dist/index.mjs +2 -4
- package/miniprogram_dist/index.d.ts +1 -1
- package/miniprogram_dist/index.js +361 -28
- package/package.json +4 -5
- package/tsup.config.miniprogram.ts +1 -1
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,14 @@
|
|
2
2
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
4
4
|
|
5
|
+
### [1.10.1](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.10.0...v1.10.1) (2025-04-27)
|
6
|
+
|
7
|
+
|
8
|
+
### Bug Fixes
|
9
|
+
|
10
|
+
* miniprogram build issue ([5e8ece4](https://github.com/Cubelrti/sm-crypto-v2/commit/5e8ece41dab69fc449675f9d46dfd25163538a78))
|
11
|
+
* **sm2:** typing issue ([f368a72](https://github.com/Cubelrti/sm-crypto-v2/commit/f368a72cc354d0ec9601dda8cbd2590eb9e6c144))
|
12
|
+
|
5
13
|
## [1.10.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.9.3...v1.10.0) (2025-04-14)
|
6
14
|
|
7
15
|
|
package/README.md
CHANGED
@@ -19,6 +19,7 @@ For WebAssembly-supported platform, see [sm-crypto-wasm](https://github.com/Cube
|
|
19
19
|
- 🎲 自动选择最优的安全随机数实现,避免使用 `Math.random()` 和 `Date.now()` 进行模拟
|
20
20
|
- 📚 同时导出 ES Module 和 CommonJS 两种格式,可按需使用
|
21
21
|
- 🔑 提供 SM2 密钥交换 API
|
22
|
+
- 🔒 提供 SM4 GCM 模式加密解密能力
|
22
23
|
- 🎒 未压缩大小 34kb,压缩后 17kb
|
23
24
|
|
24
25
|
## 安装
|
@@ -171,6 +172,14 @@ let encryptData = sm4.encrypt(msg, key) // 加密,默认输出 16 进制字符
|
|
171
172
|
let encryptData = sm4.encrypt(msg, key, {padding: 'none'}) // 加密,不使用 padding
|
172
173
|
let encryptData = sm4.encrypt(msg, key, {padding: 'none', output: 'array'}) // 加密,不使用 padding,输出为字节数组
|
173
174
|
let encryptData = sm4.encrypt(msg, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 加密,cbc 模式
|
175
|
+
let encryptData = sm4.encrypt(msg, key, {
|
176
|
+
mode: 'gcm', // gcm 模式,必填 iv, 可选 aad
|
177
|
+
iv,
|
178
|
+
associatedData,
|
179
|
+
output: 'string',
|
180
|
+
outputTag: true, // 输出 aead tag,如果为 false 则不输出
|
181
|
+
}) // 输出格式 { output: T; tag?: T; } T 为 string/Uint8Array
|
182
|
+
|
174
183
|
```
|
175
184
|
|
176
185
|
### 解密
|
@@ -184,6 +193,13 @@ let decryptData = sm4.decrypt(encryptData, key) // 解密,默认输出 utf8
|
|
184
193
|
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none'}) // 解密,不使用 padding
|
185
194
|
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array'}) // 解密,不使用 padding,输出为字节数组
|
186
195
|
let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密,cbc 模式
|
196
|
+
let decryptData = sm4.decrypt(encryptData, key, {
|
197
|
+
mode: 'gcm', // gcm 模式必填 iv tag, 可选 aad
|
198
|
+
iv,
|
199
|
+
associatedData,
|
200
|
+
tag: expectedAuthTag,
|
201
|
+
output: 'array'
|
202
|
+
}) // 输出格式 string/Uint8Array
|
187
203
|
```
|
188
204
|
|
189
205
|
### 密钥交换
|
package/dist/index.d.mts
CHANGED
@@ -60,7 +60,7 @@ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?:
|
|
60
60
|
asn1?: boolean;
|
61
61
|
}): Uint8Array;
|
62
62
|
declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
|
63
|
-
output
|
63
|
+
output?: 'string';
|
64
64
|
asn1?: boolean;
|
65
65
|
}): string;
|
66
66
|
interface SignaturePoint {
|
package/dist/index.d.ts
CHANGED
@@ -60,7 +60,7 @@ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?:
|
|
60
60
|
asn1?: boolean;
|
61
61
|
}): Uint8Array;
|
62
62
|
declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
|
63
|
-
output
|
63
|
+
output?: 'string';
|
64
64
|
asn1?: boolean;
|
65
65
|
}): string;
|
66
66
|
interface SignaturePoint {
|
package/dist/index.js
CHANGED
@@ -800,10 +800,8 @@ function xorCipherStream(x2, y2, msg) {
|
|
800
800
|
msg[i] ^= t[offset++] & 255;
|
801
801
|
}
|
802
802
|
}
|
803
|
-
function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
804
|
-
output = "string",
|
805
|
-
asn1 = false
|
806
|
-
} = {}) {
|
803
|
+
function doDecrypt(encryptData, privateKey, cipherMode = 1, options) {
|
804
|
+
const { output = "string", asn1 = false } = options || {};
|
807
805
|
const privateKeyInteger = utils4.hexToNumber(privateKey);
|
808
806
|
let c1;
|
809
807
|
let c2;
|
package/dist/index.mjs
CHANGED
@@ -768,10 +768,8 @@ function xorCipherStream(x2, y2, msg) {
|
|
768
768
|
msg[i] ^= t[offset++] & 255;
|
769
769
|
}
|
770
770
|
}
|
771
|
-
function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
772
|
-
output = "string",
|
773
|
-
asn1 = false
|
774
|
-
} = {}) {
|
771
|
+
function doDecrypt(encryptData, privateKey, cipherMode = 1, options) {
|
772
|
+
const { output = "string", asn1 = false } = options || {};
|
775
773
|
const privateKeyInteger = utils4.hexToNumber(privateKey);
|
776
774
|
let c1;
|
777
775
|
let c2;
|
@@ -60,7 +60,7 @@ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?:
|
|
60
60
|
asn1?: boolean;
|
61
61
|
}): Uint8Array;
|
62
62
|
declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
|
63
|
-
output
|
63
|
+
output?: 'string';
|
64
64
|
asn1?: boolean;
|
65
65
|
}): string;
|
66
66
|
interface SignaturePoint {
|
@@ -1227,7 +1227,7 @@ function nLength(n, nBitLength) {
|
|
1227
1227
|
};
|
1228
1228
|
}
|
1229
1229
|
function Field(ORDER, bitLen2) {
|
1230
|
-
var
|
1230
|
+
var isLE3 = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false, redef = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : {};
|
1231
1231
|
if (ORDER <= _0n2) throw new Error("Expected Fp ORDER > 0, got ".concat(ORDER));
|
1232
1232
|
var _nLength = nLength(ORDER, bitLen2), BITS = _nLength.nBitLength, BYTES = _nLength.nByteLength;
|
1233
1233
|
if (BYTES > 2048) throw new Error("Field lengths over 2048 bytes are not supported");
|
@@ -1304,22 +1304,22 @@ function Field(ORDER, bitLen2) {
|
|
1304
1304
|
return c ? b : a;
|
1305
1305
|
},
|
1306
1306
|
toBytes: function(num) {
|
1307
|
-
return
|
1307
|
+
return isLE3 ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES);
|
1308
1308
|
},
|
1309
1309
|
fromBytes: function(bytes) {
|
1310
1310
|
if (bytes.length !== BYTES) throw new Error("Fp.fromBytes: expected ".concat(BYTES, ", got ").concat(bytes.length));
|
1311
|
-
return
|
1311
|
+
return isLE3 ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
1312
1312
|
}
|
1313
1313
|
});
|
1314
1314
|
return Object.freeze(f);
|
1315
1315
|
}
|
1316
1316
|
function hashToPrivateScalar(hash, groupOrder) {
|
1317
|
-
var
|
1317
|
+
var isLE3 = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
|
1318
1318
|
hash = ensureBytes("privateHash", hash);
|
1319
1319
|
var hashLen = hash.length;
|
1320
1320
|
var minLen = nLength(groupOrder).nByteLength + 8;
|
1321
1321
|
if (minLen < 24 || hashLen < minLen || hashLen > 1024) throw new Error("hashToPrivateScalar: expected ".concat(minLen, "-1024 bytes of input, got ").concat(hashLen));
|
1322
|
-
var num =
|
1322
|
+
var num = isLE3 ? bytesToNumberLE(hash) : bytesToNumberBE(hash);
|
1323
1323
|
return mod(num, groupOrder - _1n2) + _1n2;
|
1324
1324
|
}
|
1325
1325
|
// node_modules/.pnpm/@noble+curves@1.1.0/node_modules/@noble/curves/esm/abstract/curve.js
|
@@ -1564,7 +1564,7 @@ function weierstrassPoints(opts) {
|
|
1564
1564
|
};
|
1565
1565
|
var CURVE = validatePointOpts(opts);
|
1566
1566
|
var Fp = CURVE.Fp;
|
1567
|
-
var
|
1567
|
+
var toBytes3 = CURVE.toBytes || function(c, point, isCompressed) {
|
1568
1568
|
var a = point.toAffine();
|
1569
1569
|
return concatBytes(Uint8Array.from([
|
1570
1570
|
4
|
@@ -1910,7 +1910,7 @@ function weierstrassPoints(opts) {
|
|
1910
1910
|
value: function toRawBytes() {
|
1911
1911
|
var isCompressed = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : true;
|
1912
1912
|
this.assertValidity();
|
1913
|
-
return
|
1913
|
+
return toBytes3(Point, this, isCompressed);
|
1914
1914
|
}
|
1915
1915
|
},
|
1916
1916
|
{
|
@@ -2542,16 +2542,16 @@ var BoolB = function(A, B, C) {
|
|
2542
2542
|
var BoolC = function(A, B, C) {
|
2543
2543
|
return A & B | ~A & C;
|
2544
2544
|
};
|
2545
|
-
function setBigUint64(view, byteOffset, value,
|
2546
|
-
if (typeof view.setBigUint64 === "function") return view.setBigUint64(byteOffset, value,
|
2545
|
+
function setBigUint64(view, byteOffset, value, isLE3) {
|
2546
|
+
if (typeof view.setBigUint64 === "function") return view.setBigUint64(byteOffset, value, isLE3);
|
2547
2547
|
var _32n = BigInt(32);
|
2548
2548
|
var _u32_max = BigInt(4294967295);
|
2549
2549
|
var wh = Number(value >> _32n & _u32_max);
|
2550
2550
|
var wl = Number(value & _u32_max);
|
2551
|
-
var h =
|
2552
|
-
var l =
|
2553
|
-
view.setUint32(byteOffset + h, wh,
|
2554
|
-
view.setUint32(byteOffset + l, wl,
|
2551
|
+
var h = isLE3 ? 4 : 0;
|
2552
|
+
var l = isLE3 ? 0 : 4;
|
2553
|
+
view.setUint32(byteOffset + h, wh, isLE3);
|
2554
|
+
view.setUint32(byteOffset + l, wl, isLE3);
|
2555
2555
|
}
|
2556
2556
|
function rotl(x2, n) {
|
2557
2557
|
var s = n & 31;
|
@@ -2566,14 +2566,14 @@ function P1(X) {
|
|
2566
2566
|
var SHA2 = /*#__PURE__*/ function(Hash) {
|
2567
2567
|
_inherits(SHA2, Hash);
|
2568
2568
|
var _super = _create_super(SHA2);
|
2569
|
-
function SHA2(blockLen, outputLen, padOffset,
|
2569
|
+
function SHA2(blockLen, outputLen, padOffset, isLE3) {
|
2570
2570
|
_class_call_check(this, SHA2);
|
2571
2571
|
var _this;
|
2572
2572
|
_this = _super.call(this);
|
2573
2573
|
_this.blockLen = blockLen;
|
2574
2574
|
_this.outputLen = outputLen;
|
2575
2575
|
_this.padOffset = padOffset;
|
2576
|
-
_this.isLE =
|
2576
|
+
_this.isLE = isLE3;
|
2577
2577
|
// For partial updates less than block size
|
2578
2578
|
__publicField(_assert_this_initialized(_this), "buffer");
|
2579
2579
|
__publicField(_assert_this_initialized(_this), "view");
|
@@ -2616,7 +2616,7 @@ var SHA2 = /*#__PURE__*/ function(Hash) {
|
|
2616
2616
|
key: "digestInto",
|
2617
2617
|
value: function digestInto(out) {
|
2618
2618
|
this.finished = true;
|
2619
|
-
var _this = this, buffer = _this.buffer, view = _this.view, blockLen = _this.blockLen,
|
2619
|
+
var _this = this, buffer = _this.buffer, view = _this.view, blockLen = _this.blockLen, isLE3 = _this.isLE;
|
2620
2620
|
var pos = this.pos;
|
2621
2621
|
buffer[pos++] = 128;
|
2622
2622
|
this.buffer.subarray(pos).fill(0);
|
@@ -2625,7 +2625,7 @@ var SHA2 = /*#__PURE__*/ function(Hash) {
|
|
2625
2625
|
pos = 0;
|
2626
2626
|
}
|
2627
2627
|
for(var i = pos; i < blockLen; i++)buffer[i] = 0;
|
2628
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8),
|
2628
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE3);
|
2629
2629
|
this.process(view, 0);
|
2630
2630
|
var oview = createView(out);
|
2631
2631
|
var len = this.outputLen;
|
@@ -2633,7 +2633,7 @@ var SHA2 = /*#__PURE__*/ function(Hash) {
|
|
2633
2633
|
var outLen = len / 4;
|
2634
2634
|
var state = this.get();
|
2635
2635
|
if (outLen > state.length) throw new Error("_sha2: outputLen bigger than state");
|
2636
|
-
for(var i1 = 0; i1 < outLen; i1++)oview.setUint32(4 * i1, state[i1],
|
2636
|
+
for(var i1 = 0; i1 < outLen; i1++)oview.setUint32(4 * i1, state[i1], isLE3);
|
2637
2637
|
}
|
2638
2638
|
},
|
2639
2639
|
{
|
@@ -3073,7 +3073,8 @@ function xorCipherStream(x2, y2, msg) {
|
|
3073
3073
|
}
|
3074
3074
|
}
|
3075
3075
|
function doDecrypt(encryptData, privateKey) {
|
3076
|
-
var cipherMode = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1,
|
3076
|
+
var cipherMode = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1, options = arguments.length > 3 ? arguments[3] : void 0;
|
3077
|
+
var _ref = options || {}, _ref_output = _ref.output, output = _ref_output === void 0 ? "string" : _ref_output, _ref_asn1 = _ref.asn1, asn1 = _ref_asn1 === void 0 ? false : _ref_asn1;
|
3077
3078
|
var privateKeyInteger = hexToNumber(privateKey);
|
3078
3079
|
var c1;
|
3079
3080
|
var c2;
|
@@ -3271,8 +3272,337 @@ __export(sm4_exports, {
|
|
3271
3272
|
return sm4;
|
3272
3273
|
}
|
3273
3274
|
});
|
3274
|
-
|
3275
|
-
|
3275
|
+
// node_modules/.pnpm/@noble+ciphers@1.2.1/node_modules/@noble/ciphers/esm/_assert.js
|
3276
|
+
function isBytes(a) {
|
3277
|
+
return _instanceof(a, Uint8Array) || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
3278
|
+
}
|
3279
|
+
function abytes(b) {
|
3280
|
+
for(var _len = arguments.length, lengths = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){
|
3281
|
+
lengths[_key - 1] = arguments[_key];
|
3282
|
+
}
|
3283
|
+
if (!isBytes(b)) throw new Error("Uint8Array expected");
|
3284
|
+
if (lengths.length > 0 && !lengths.includes(b.length)) throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
3285
|
+
}
|
3286
|
+
function aexists(instance) {
|
3287
|
+
var checkFinished = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true;
|
3288
|
+
if (instance.destroyed) throw new Error("Hash instance has been destroyed");
|
3289
|
+
if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called");
|
3290
|
+
}
|
3291
|
+
function aoutput(out, instance) {
|
3292
|
+
abytes(out);
|
3293
|
+
var min = instance.outputLen;
|
3294
|
+
if (out.length < min) {
|
3295
|
+
throw new Error("digestInto() expects output buffer of length at least " + min);
|
3296
|
+
}
|
3297
|
+
}
|
3298
|
+
// node_modules/.pnpm/@noble+ciphers@1.2.1/node_modules/@noble/ciphers/esm/utils.js
|
3299
|
+
var u32 = function(arr) {
|
3300
|
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
3301
|
+
};
|
3302
|
+
var createView2 = function(arr) {
|
3303
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
3304
|
+
};
|
3305
|
+
var isLE2 = new Uint8Array(new Uint32Array([
|
3306
|
+
287454020
|
3307
|
+
]).buffer)[0] === 68;
|
3308
|
+
if (!isLE2) throw new Error("Non little-endian hardware is not supported");
|
3309
|
+
function utf8ToBytes3(str) {
|
3310
|
+
if (typeof str !== "string") throw new Error("string expected");
|
3311
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
3312
|
+
}
|
3313
|
+
function toBytes2(data) {
|
3314
|
+
if (typeof data === "string") data = utf8ToBytes3(data);
|
3315
|
+
else if (isBytes(data)) data = copyBytes(data);
|
3316
|
+
else throw new Error("Uint8Array expected, got " + (typeof data === "undefined" ? "undefined" : _type_of(data)));
|
3317
|
+
return data;
|
3318
|
+
}
|
3319
|
+
function setBigUint642(view, byteOffset, value, isLE3) {
|
3320
|
+
if (typeof view.setBigUint64 === "function") return view.setBigUint64(byteOffset, value, isLE3);
|
3321
|
+
var _32n = BigInt(32);
|
3322
|
+
var _u32_max = BigInt(4294967295);
|
3323
|
+
var wh = Number(value >> _32n & _u32_max);
|
3324
|
+
var wl = Number(value & _u32_max);
|
3325
|
+
var h = isLE3 ? 4 : 0;
|
3326
|
+
var l = isLE3 ? 0 : 4;
|
3327
|
+
view.setUint32(byteOffset + h, wh, isLE3);
|
3328
|
+
view.setUint32(byteOffset + l, wl, isLE3);
|
3329
|
+
}
|
3330
|
+
function copyBytes(bytes) {
|
3331
|
+
return Uint8Array.from(bytes);
|
3332
|
+
}
|
3333
|
+
function clean() {
|
3334
|
+
for(var _len = arguments.length, arrays = new Array(_len), _key = 0; _key < _len; _key++){
|
3335
|
+
arrays[_key] = arguments[_key];
|
3336
|
+
}
|
3337
|
+
for(var i = 0; i < arrays.length; i++){
|
3338
|
+
arrays[i].fill(0);
|
3339
|
+
}
|
3340
|
+
}
|
3341
|
+
// node_modules/.pnpm/@noble+ciphers@1.2.1/node_modules/@noble/ciphers/esm/_polyval.js
|
3342
|
+
var BLOCK_SIZE = 16;
|
3343
|
+
var ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
|
3344
|
+
var ZEROS32 = u32(ZEROS16);
|
3345
|
+
var POLY = 225;
|
3346
|
+
var mul2 = function(s0, s1, s2, s3) {
|
3347
|
+
var hiBit = s3 & 1;
|
3348
|
+
return {
|
3349
|
+
s3: s2 << 31 | s3 >>> 1,
|
3350
|
+
s2: s1 << 31 | s2 >>> 1,
|
3351
|
+
s1: s0 << 31 | s1 >>> 1,
|
3352
|
+
s0: s0 >>> 1 ^ POLY << 24 & -(hiBit & 1)
|
3353
|
+
};
|
3354
|
+
};
|
3355
|
+
var swapLE = function(n) {
|
3356
|
+
return (n >>> 0 & 255) << 24 | (n >>> 8 & 255) << 16 | (n >>> 16 & 255) << 8 | n >>> 24 & 255 | 0;
|
3357
|
+
};
|
3358
|
+
function _toGHASHKey(k) {
|
3359
|
+
k.reverse();
|
3360
|
+
var hiBit = k[15] & 1;
|
3361
|
+
var carry = 0;
|
3362
|
+
for(var i = 0; i < k.length; i++){
|
3363
|
+
var t = k[i];
|
3364
|
+
k[i] = t >>> 1 | carry;
|
3365
|
+
carry = (t & 1) << 7;
|
3366
|
+
}
|
3367
|
+
k[0] ^= -hiBit & 225;
|
3368
|
+
return k;
|
3369
|
+
}
|
3370
|
+
var estimateWindow = function(bytes) {
|
3371
|
+
if (bytes > 64 * 1024) return 8;
|
3372
|
+
if (bytes > 1024) return 4;
|
3373
|
+
return 2;
|
3374
|
+
};
|
3375
|
+
var GHASH = /*#__PURE__*/ function() {
|
3376
|
+
function GHASH(key, expectedLength) {
|
3377
|
+
_class_call_check(this, GHASH);
|
3378
|
+
this.blockLen = BLOCK_SIZE;
|
3379
|
+
this.outputLen = BLOCK_SIZE;
|
3380
|
+
this.s0 = 0;
|
3381
|
+
this.s1 = 0;
|
3382
|
+
this.s2 = 0;
|
3383
|
+
this.s3 = 0;
|
3384
|
+
this.finished = false;
|
3385
|
+
key = toBytes2(key);
|
3386
|
+
abytes(key, 16);
|
3387
|
+
var kView = createView2(key);
|
3388
|
+
var k0 = kView.getUint32(0, false);
|
3389
|
+
var k1 = kView.getUint32(4, false);
|
3390
|
+
var k2 = kView.getUint32(8, false);
|
3391
|
+
var k3 = kView.getUint32(12, false);
|
3392
|
+
var doubles = [];
|
3393
|
+
for(var i = 0; i < 128; i++){
|
3394
|
+
doubles.push({
|
3395
|
+
s0: swapLE(k0),
|
3396
|
+
s1: swapLE(k1),
|
3397
|
+
s2: swapLE(k2),
|
3398
|
+
s3: swapLE(k3)
|
3399
|
+
});
|
3400
|
+
var ref;
|
3401
|
+
ref = mul2(k0, k1, k2, k3), k0 = ref.s0, k1 = ref.s1, k2 = ref.s2, k3 = ref.s3, ref;
|
3402
|
+
}
|
3403
|
+
var W = estimateWindow(expectedLength || 1024);
|
3404
|
+
if (![
|
3405
|
+
1,
|
3406
|
+
2,
|
3407
|
+
4,
|
3408
|
+
8
|
3409
|
+
].includes(W)) throw new Error("ghash: invalid window size, expected 2, 4 or 8");
|
3410
|
+
this.W = W;
|
3411
|
+
var bits = 128;
|
3412
|
+
var windows = bits / W;
|
3413
|
+
var windowSize = this.windowSize = Math.pow(2, W);
|
3414
|
+
var items = [];
|
3415
|
+
for(var w = 0; w < windows; w++){
|
3416
|
+
for(var byte = 0; byte < windowSize; byte++){
|
3417
|
+
var s0 = 0, s1 = 0, s2 = 0, s3 = 0;
|
3418
|
+
for(var j = 0; j < W; j++){
|
3419
|
+
var bit = byte >>> W - j - 1 & 1;
|
3420
|
+
if (!bit) continue;
|
3421
|
+
var _doubles_ = doubles[W * w + j], d0 = _doubles_.s0, d1 = _doubles_.s1, d2 = _doubles_.s2, d3 = _doubles_.s3;
|
3422
|
+
s0 ^= d0, s1 ^= d1, s2 ^= d2, s3 ^= d3;
|
3423
|
+
}
|
3424
|
+
items.push({
|
3425
|
+
s0: s0,
|
3426
|
+
s1: s1,
|
3427
|
+
s2: s2,
|
3428
|
+
s3: s3
|
3429
|
+
});
|
3430
|
+
}
|
3431
|
+
}
|
3432
|
+
this.t = items;
|
3433
|
+
}
|
3434
|
+
_create_class(GHASH, [
|
3435
|
+
{
|
3436
|
+
key: "_updateBlock",
|
3437
|
+
value: function _updateBlock(s0, s1, s2, s3) {
|
3438
|
+
s0 ^= this.s0, s1 ^= this.s1, s2 ^= this.s2, s3 ^= this.s3;
|
3439
|
+
var _this = this, W = _this.W, t = _this.t, windowSize = _this.windowSize;
|
3440
|
+
var o0 = 0, o1 = 0, o2 = 0, o3 = 0;
|
3441
|
+
var mask = (1 << W) - 1;
|
3442
|
+
var w = 0;
|
3443
|
+
for(var _i = 0, _iter = [
|
3444
|
+
s0,
|
3445
|
+
s1,
|
3446
|
+
s2,
|
3447
|
+
s3
|
3448
|
+
]; _i < _iter.length; _i++){
|
3449
|
+
var num = _iter[_i];
|
3450
|
+
for(var bytePos = 0; bytePos < 4; bytePos++){
|
3451
|
+
var byte = num >>> 8 * bytePos & 255;
|
3452
|
+
for(var bitPos = 8 / W - 1; bitPos >= 0; bitPos--){
|
3453
|
+
var bit = byte >>> W * bitPos & mask;
|
3454
|
+
var _t_ = t[w * windowSize + bit], e0 = _t_.s0, e1 = _t_.s1, e2 = _t_.s2, e3 = _t_.s3;
|
3455
|
+
o0 ^= e0, o1 ^= e1, o2 ^= e2, o3 ^= e3;
|
3456
|
+
w += 1;
|
3457
|
+
}
|
3458
|
+
}
|
3459
|
+
}
|
3460
|
+
this.s0 = o0;
|
3461
|
+
this.s1 = o1;
|
3462
|
+
this.s2 = o2;
|
3463
|
+
this.s3 = o3;
|
3464
|
+
}
|
3465
|
+
},
|
3466
|
+
{
|
3467
|
+
key: "update",
|
3468
|
+
value: function update(data) {
|
3469
|
+
data = toBytes2(data);
|
3470
|
+
aexists(this);
|
3471
|
+
var b32 = u32(data);
|
3472
|
+
var blocks = Math.floor(data.length / BLOCK_SIZE);
|
3473
|
+
var left = data.length % BLOCK_SIZE;
|
3474
|
+
for(var i = 0; i < blocks; i++){
|
3475
|
+
this._updateBlock(b32[i * 4 + 0], b32[i * 4 + 1], b32[i * 4 + 2], b32[i * 4 + 3]);
|
3476
|
+
}
|
3477
|
+
if (left) {
|
3478
|
+
ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
|
3479
|
+
this._updateBlock(ZEROS32[0], ZEROS32[1], ZEROS32[2], ZEROS32[3]);
|
3480
|
+
clean(ZEROS32);
|
3481
|
+
}
|
3482
|
+
return this;
|
3483
|
+
}
|
3484
|
+
},
|
3485
|
+
{
|
3486
|
+
key: "destroy",
|
3487
|
+
value: function destroy() {
|
3488
|
+
var t = this.t;
|
3489
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
3490
|
+
try {
|
3491
|
+
for(var _iterator = t[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
3492
|
+
var elm = _step.value;
|
3493
|
+
elm.s0 = 0, elm.s1 = 0, elm.s2 = 0, elm.s3 = 0;
|
3494
|
+
}
|
3495
|
+
} catch (err) {
|
3496
|
+
_didIteratorError = true;
|
3497
|
+
_iteratorError = err;
|
3498
|
+
} finally{
|
3499
|
+
try {
|
3500
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
3501
|
+
_iterator.return();
|
3502
|
+
}
|
3503
|
+
} finally{
|
3504
|
+
if (_didIteratorError) {
|
3505
|
+
throw _iteratorError;
|
3506
|
+
}
|
3507
|
+
}
|
3508
|
+
}
|
3509
|
+
}
|
3510
|
+
},
|
3511
|
+
{
|
3512
|
+
key: "digestInto",
|
3513
|
+
value: function digestInto(out) {
|
3514
|
+
aexists(this);
|
3515
|
+
aoutput(out, this);
|
3516
|
+
this.finished = true;
|
3517
|
+
var _this = this, s0 = _this.s0, s1 = _this.s1, s2 = _this.s2, s3 = _this.s3;
|
3518
|
+
var o32 = u32(out);
|
3519
|
+
o32[0] = s0;
|
3520
|
+
o32[1] = s1;
|
3521
|
+
o32[2] = s2;
|
3522
|
+
o32[3] = s3;
|
3523
|
+
return out;
|
3524
|
+
}
|
3525
|
+
},
|
3526
|
+
{
|
3527
|
+
key: "digest",
|
3528
|
+
value: function digest() {
|
3529
|
+
var res = new Uint8Array(BLOCK_SIZE);
|
3530
|
+
this.digestInto(res);
|
3531
|
+
this.destroy();
|
3532
|
+
return res;
|
3533
|
+
}
|
3534
|
+
}
|
3535
|
+
]);
|
3536
|
+
return GHASH;
|
3537
|
+
}();
|
3538
|
+
var Polyval = /*#__PURE__*/ function(GHASH) {
|
3539
|
+
_inherits(Polyval, GHASH);
|
3540
|
+
var _super = _create_super(Polyval);
|
3541
|
+
function Polyval(key, expectedLength) {
|
3542
|
+
_class_call_check(this, Polyval);
|
3543
|
+
key = toBytes2(key);
|
3544
|
+
var ghKey = _toGHASHKey(copyBytes(key));
|
3545
|
+
var _this = _super.call(this, ghKey, expectedLength);
|
3546
|
+
clean(ghKey);
|
3547
|
+
return _this;
|
3548
|
+
}
|
3549
|
+
_create_class(Polyval, [
|
3550
|
+
{
|
3551
|
+
key: "update",
|
3552
|
+
value: function update(data) {
|
3553
|
+
data = toBytes2(data);
|
3554
|
+
aexists(this);
|
3555
|
+
var b32 = u32(data);
|
3556
|
+
var left = data.length % BLOCK_SIZE;
|
3557
|
+
var blocks = Math.floor(data.length / BLOCK_SIZE);
|
3558
|
+
for(var i = 0; i < blocks; i++){
|
3559
|
+
this._updateBlock(swapLE(b32[i * 4 + 3]), swapLE(b32[i * 4 + 2]), swapLE(b32[i * 4 + 1]), swapLE(b32[i * 4 + 0]));
|
3560
|
+
}
|
3561
|
+
if (left) {
|
3562
|
+
ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
|
3563
|
+
this._updateBlock(swapLE(ZEROS32[3]), swapLE(ZEROS32[2]), swapLE(ZEROS32[1]), swapLE(ZEROS32[0]));
|
3564
|
+
clean(ZEROS32);
|
3565
|
+
}
|
3566
|
+
return this;
|
3567
|
+
}
|
3568
|
+
},
|
3569
|
+
{
|
3570
|
+
key: "digestInto",
|
3571
|
+
value: function digestInto(out) {
|
3572
|
+
aexists(this);
|
3573
|
+
aoutput(out, this);
|
3574
|
+
this.finished = true;
|
3575
|
+
var _this = this, s0 = _this.s0, s1 = _this.s1, s2 = _this.s2, s3 = _this.s3;
|
3576
|
+
var o32 = u32(out);
|
3577
|
+
o32[0] = s0;
|
3578
|
+
o32[1] = s1;
|
3579
|
+
o32[2] = s2;
|
3580
|
+
o32[3] = s3;
|
3581
|
+
return out.reverse();
|
3582
|
+
}
|
3583
|
+
}
|
3584
|
+
]);
|
3585
|
+
return Polyval;
|
3586
|
+
}(GHASH);
|
3587
|
+
function wrapConstructorWithKey(hashCons) {
|
3588
|
+
var hashC = function(msg, key) {
|
3589
|
+
return hashCons(key, msg.length).update(toBytes2(msg)).digest();
|
3590
|
+
};
|
3591
|
+
var tmp2 = hashCons(new Uint8Array(16), 0);
|
3592
|
+
hashC.outputLen = tmp2.outputLen;
|
3593
|
+
hashC.blockLen = tmp2.blockLen;
|
3594
|
+
hashC.create = function(key, expectedLength) {
|
3595
|
+
return hashCons(key, expectedLength);
|
3596
|
+
};
|
3597
|
+
return hashC;
|
3598
|
+
}
|
3599
|
+
var ghash = wrapConstructorWithKey(function(key, expectedLength) {
|
3600
|
+
return new GHASH(key, expectedLength);
|
3601
|
+
});
|
3602
|
+
var polyval = wrapConstructorWithKey(function(key, expectedLength) {
|
3603
|
+
return new Polyval(key, expectedLength);
|
3604
|
+
});
|
3605
|
+
// src/sm4/index.ts
|
3276
3606
|
var DECRYPT = 0;
|
3277
3607
|
var ROUND = 32;
|
3278
3608
|
var BLOCK = 16;
|
@@ -3683,11 +4013,11 @@ function sm4Gcm(inArray, key, ivArray, aadArray, cryptFlag, tagArray) {
|
|
3683
4013
|
j02.set(ivArray, 0);
|
3684
4014
|
j02[15] = 1;
|
3685
4015
|
} else {
|
3686
|
-
var g =
|
4016
|
+
var g = ghash.create(h2);
|
3687
4017
|
g.update(ivArray);
|
3688
4018
|
var lenIv = new Uint8Array(16);
|
3689
|
-
var view = (
|
3690
|
-
(
|
4019
|
+
var view = createView2(lenIv);
|
4020
|
+
setBigUint642(view, 8, BigInt(ivArray.length * 8), false);
|
3691
4021
|
g.update(lenIv);
|
3692
4022
|
j02 = g.digest();
|
3693
4023
|
}
|
@@ -3706,15 +4036,15 @@ function sm4Gcm(inArray, key, ivArray, aadArray, cryptFlag, tagArray) {
|
|
3706
4036
|
var computeTag = function computeTag(h2, data) {
|
3707
4037
|
var aadLength = aadArray.length;
|
3708
4038
|
var dataLength = data.length;
|
3709
|
-
var g =
|
4039
|
+
var g = ghash.create(h2);
|
3710
4040
|
if (aadLength > 0) {
|
3711
4041
|
g.update(aadArray);
|
3712
4042
|
}
|
3713
4043
|
g.update(data);
|
3714
4044
|
var lenBlock = new Uint8Array(16);
|
3715
|
-
var view = (
|
3716
|
-
(
|
3717
|
-
(
|
4045
|
+
var view = createView2(lenBlock);
|
4046
|
+
setBigUint642(view, 0, BigInt(aadLength * 8), false);
|
4047
|
+
setBigUint642(view, 8, BigInt(dataLength * 8), false);
|
3718
4048
|
g.update(lenBlock);
|
3719
4049
|
return g.digest();
|
3720
4050
|
};
|
@@ -3913,4 +4243,7 @@ function decrypt(inArray, key) {
|
|
3913
4243
|
|
3914
4244
|
@noble/curves/esm/abstract/weierstrass.js:
|
3915
4245
|
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
4246
|
+
|
4247
|
+
@noble/ciphers/esm/utils.js:
|
4248
|
+
(*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
|
3916
4249
|
*/
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "sm-crypto-v2",
|
3
|
-
"version": "1.10.
|
3
|
+
"version": "1.10.1",
|
4
4
|
"description": "sm-crypto-v2",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"module": "dist/index.mjs",
|
@@ -36,6 +36,9 @@
|
|
36
36
|
"@noble/ciphers": "^1.2.1",
|
37
37
|
"@noble/curves": "^1.1.0"
|
38
38
|
},
|
39
|
+
"publishConfig": {
|
40
|
+
"registry": "https://registry.npmjs.org/"
|
41
|
+
},
|
39
42
|
"devDependencies": {
|
40
43
|
"@swc-node/register": "^1.6.6",
|
41
44
|
"@swc/core": "^1.3.62",
|
@@ -58,9 +61,5 @@
|
|
58
61
|
"typescript": "^4.7.2",
|
59
62
|
"vite": "^4.3.9",
|
60
63
|
"vitest": "^0.31.0"
|
61
|
-
},
|
62
|
-
"publishConfig": {
|
63
|
-
"registry": "https://registry.npmjs.org/",
|
64
|
-
"access": "public"
|
65
64
|
}
|
66
65
|
}
|
@@ -10,7 +10,7 @@ export default defineConfig({
|
|
10
10
|
minify: false,
|
11
11
|
format: ['cjs'],
|
12
12
|
target: 'es5',
|
13
|
-
noExternal: ['@noble/curves'],
|
13
|
+
noExternal: ['@noble/curves', '@noble/ciphers'],
|
14
14
|
tsconfig: 'tsconfig.json',
|
15
15
|
esbuildOptions(options) {
|
16
16
|
options.define.__BUILD_TS__ = Date.now().toString();
|