sm-crypto-v2 1.11.0 → 1.12.0

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 CHANGED
@@ -2,6 +2,13 @@
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.12.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.11.0...v1.12.0) (2025-06-05)
6
+
7
+
8
+ ### Features
9
+
10
+ * **utils:** ensure sm2 runs in environment don't have TextEncoder ([6b74b8d](https://github.com/Cubelrti/sm-crypto-v2/commit/6b74b8d4df3ead8e2c01587e3c13cdbbf9a27701))
11
+
5
12
  ## [1.11.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.10.1...v1.11.0) (2025-04-28)
6
13
 
7
14
 
package/dist/index.js CHANGED
@@ -304,7 +304,10 @@ var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLengt
304
304
  var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
305
305
  if (!isLE)
306
306
  throw new Error("Non little-endian hardware is not supported");
307
- var hexes = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, "0"));
307
+ var hexes = Array.from(
308
+ { length: 256 },
309
+ (v, i) => i.toString(16).padStart(2, "0")
310
+ );
308
311
  function bytesToHex(bytes) {
309
312
  if (!u8a(bytes))
310
313
  throw new Error("Uint8Array expected");
@@ -314,14 +317,44 @@ function bytesToHex(bytes) {
314
317
  }
315
318
  return hex;
316
319
  }
317
- function utf8ToBytes(str) {
318
- if (typeof str !== "string")
319
- throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
320
- return new Uint8Array(new TextEncoder().encode(str));
320
+ var te = typeof TextEncoder != "undefined" && /* @__PURE__ */ new TextEncoder();
321
+ var slc = (v, s, e) => {
322
+ if (s == null || s < 0)
323
+ s = 0;
324
+ if (e == null || e > v.length)
325
+ e = v.length;
326
+ return new Uint8Array(v.subarray(s, e));
327
+ };
328
+ function strToU8(str) {
329
+ if (te)
330
+ return te.encode(str);
331
+ const l = str.length;
332
+ let ar = new Uint8Array(str.length + (str.length >> 1));
333
+ let ai = 0;
334
+ const w = (v) => {
335
+ ar[ai++] = v;
336
+ };
337
+ for (let i = 0; i < l; ++i) {
338
+ if (ai + 5 > ar.length) {
339
+ const n = new Uint8Array(ai + 8 + (l - i << 1));
340
+ n.set(ar);
341
+ ar = n;
342
+ }
343
+ let c = str.charCodeAt(i);
344
+ if (c < 128)
345
+ w(c);
346
+ else if (c < 2048)
347
+ w(192 | c >> 6), w(128 | c & 63);
348
+ else if (c > 55295 && c < 57344)
349
+ c = 65536 + (c & 1023 << 10) | str.charCodeAt(++i) & 1023, w(240 | c >> 18), w(128 | c >> 12 & 63), w(128 | c >> 6 & 63), w(128 | c & 63);
350
+ else
351
+ w(224 | c >> 12), w(128 | c >> 6 & 63), w(128 | c & 63);
352
+ }
353
+ return slc(ar, 0, ai);
321
354
  }
322
355
  function toBytes(data) {
323
356
  if (typeof data === "string")
324
- data = utf8ToBytes(data);
357
+ data = strToU8(data);
325
358
  if (!u8a(data))
326
359
  throw new Error(`expected Uint8Array, got ${typeof data}`);
327
360
  return data;
@@ -637,7 +670,7 @@ function compressPublicKeyHex(s) {
637
670
  return prefix + xHex;
638
671
  }
639
672
  function utf8ToHex(input) {
640
- const bytes = utils2.utf8ToBytes(input);
673
+ const bytes = strToU8(input);
641
674
  return utils2.bytesToHex(bytes);
642
675
  }
643
676
  function leftPad(input, num) {
@@ -989,7 +1022,7 @@ __export(sm4_exports, {
989
1022
  sm4: () => sm4
990
1023
  });
991
1024
  var import_polyval = require("@noble/ciphers/_polyval");
992
- var import_utils11 = require("@noble/ciphers/utils");
1025
+ var import_utils12 = require("@noble/ciphers/utils");
993
1026
  var DECRYPT = 0;
994
1027
  var ROUND = 32;
995
1028
  var BLOCK = 16;
@@ -1401,8 +1434,8 @@ function sm4Gcm(inArray, key, ivArray, aadArray, cryptFlag, tagArray) {
1401
1434
  const g = import_polyval.ghash.create(h2);
1402
1435
  g.update(ivArray);
1403
1436
  const lenIv = new Uint8Array(16);
1404
- const view = (0, import_utils11.createView)(lenIv);
1405
- (0, import_utils11.setBigUint64)(view, 8, BigInt(ivArray.length * 8), false);
1437
+ const view = (0, import_utils12.createView)(lenIv);
1438
+ (0, import_utils12.setBigUint64)(view, 8, BigInt(ivArray.length * 8), false);
1406
1439
  g.update(lenIv);
1407
1440
  j02 = g.digest();
1408
1441
  }
@@ -1421,9 +1454,9 @@ function sm4Gcm(inArray, key, ivArray, aadArray, cryptFlag, tagArray) {
1421
1454
  }
1422
1455
  g.update(data);
1423
1456
  const lenBlock = new Uint8Array(16);
1424
- const view = (0, import_utils11.createView)(lenBlock);
1425
- (0, import_utils11.setBigUint64)(view, 0, BigInt(aadLength * 8), false);
1426
- (0, import_utils11.setBigUint64)(view, 8, BigInt(dataLength * 8), false);
1457
+ const view = (0, import_utils12.createView)(lenBlock);
1458
+ (0, import_utils12.setBigUint64)(view, 0, BigInt(aadLength * 8), false);
1459
+ (0, import_utils12.setBigUint64)(view, 8, BigInt(dataLength * 8), false);
1427
1460
  g.update(lenBlock);
1428
1461
  return g.digest();
1429
1462
  }
package/dist/index.mjs CHANGED
@@ -271,7 +271,10 @@ var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLengt
271
271
  var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
272
272
  if (!isLE)
273
273
  throw new Error("Non little-endian hardware is not supported");
274
- var hexes = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, "0"));
274
+ var hexes = Array.from(
275
+ { length: 256 },
276
+ (v, i) => i.toString(16).padStart(2, "0")
277
+ );
275
278
  function bytesToHex(bytes) {
276
279
  if (!u8a(bytes))
277
280
  throw new Error("Uint8Array expected");
@@ -281,14 +284,44 @@ function bytesToHex(bytes) {
281
284
  }
282
285
  return hex;
283
286
  }
284
- function utf8ToBytes(str) {
285
- if (typeof str !== "string")
286
- throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
287
- return new Uint8Array(new TextEncoder().encode(str));
287
+ var te = typeof TextEncoder != "undefined" && /* @__PURE__ */ new TextEncoder();
288
+ var slc = (v, s, e) => {
289
+ if (s == null || s < 0)
290
+ s = 0;
291
+ if (e == null || e > v.length)
292
+ e = v.length;
293
+ return new Uint8Array(v.subarray(s, e));
294
+ };
295
+ function strToU8(str) {
296
+ if (te)
297
+ return te.encode(str);
298
+ const l = str.length;
299
+ let ar = new Uint8Array(str.length + (str.length >> 1));
300
+ let ai = 0;
301
+ const w = (v) => {
302
+ ar[ai++] = v;
303
+ };
304
+ for (let i = 0; i < l; ++i) {
305
+ if (ai + 5 > ar.length) {
306
+ const n = new Uint8Array(ai + 8 + (l - i << 1));
307
+ n.set(ar);
308
+ ar = n;
309
+ }
310
+ let c = str.charCodeAt(i);
311
+ if (c < 128)
312
+ w(c);
313
+ else if (c < 2048)
314
+ w(192 | c >> 6), w(128 | c & 63);
315
+ else if (c > 55295 && c < 57344)
316
+ c = 65536 + (c & 1023 << 10) | str.charCodeAt(++i) & 1023, w(240 | c >> 18), w(128 | c >> 12 & 63), w(128 | c >> 6 & 63), w(128 | c & 63);
317
+ else
318
+ w(224 | c >> 12), w(128 | c >> 6 & 63), w(128 | c & 63);
319
+ }
320
+ return slc(ar, 0, ai);
288
321
  }
289
322
  function toBytes(data) {
290
323
  if (typeof data === "string")
291
- data = utf8ToBytes(data);
324
+ data = strToU8(data);
292
325
  if (!u8a(data))
293
326
  throw new Error(`expected Uint8Array, got ${typeof data}`);
294
327
  return data;
@@ -604,7 +637,7 @@ function compressPublicKeyHex(s) {
604
637
  return prefix + xHex;
605
638
  }
606
639
  function utf8ToHex(input) {
607
- const bytes = utils2.utf8ToBytes(input);
640
+ const bytes = strToU8(input);
608
641
  return utils2.bytesToHex(bytes);
609
642
  }
610
643
  function leftPad(input, num) {
@@ -2499,12 +2499,36 @@ function bytesToHex2(bytes) {
2499
2499
  }
2500
2500
  return hex;
2501
2501
  }
2502
- function utf8ToBytes2(str) {
2503
- if (typeof str !== "string") throw new Error("utf8ToBytes expected string, got ".concat(typeof str === "undefined" ? "undefined" : _type_of(str)));
2504
- return new Uint8Array(new TextEncoder().encode(str));
2502
+ var te = typeof TextEncoder != "undefined" && /* @__PURE__ */ new TextEncoder();
2503
+ var slc = function(v, s, e) {
2504
+ if (s == null || s < 0) s = 0;
2505
+ if (e == null || e > v.length) e = v.length;
2506
+ return new Uint8Array(v.subarray(s, e));
2507
+ };
2508
+ function strToU8(str) {
2509
+ if (te) return te.encode(str);
2510
+ var l = str.length;
2511
+ var ar = new Uint8Array(str.length + (str.length >> 1));
2512
+ var ai = 0;
2513
+ var w = function(v) {
2514
+ ar[ai++] = v;
2515
+ };
2516
+ for(var i = 0; i < l; ++i){
2517
+ if (ai + 5 > ar.length) {
2518
+ var n = new Uint8Array(ai + 8 + (l - i << 1));
2519
+ n.set(ar);
2520
+ ar = n;
2521
+ }
2522
+ var c = str.charCodeAt(i);
2523
+ if (c < 128) w(c);
2524
+ else if (c < 2048) w(192 | c >> 6), w(128 | c & 63);
2525
+ else if (c > 55295 && c < 57344) c = 65536 + (c & 1023 << 10) | str.charCodeAt(++i) & 1023, w(240 | c >> 18), w(128 | c >> 12 & 63), w(128 | c >> 6 & 63), w(128 | c & 63);
2526
+ else w(224 | c >> 12), w(128 | c >> 6 & 63), w(128 | c & 63);
2527
+ }
2528
+ return slc(ar, 0, ai);
2505
2529
  }
2506
2530
  function toBytes(data) {
2507
- if (typeof data === "string") data = utf8ToBytes2(data);
2531
+ if (typeof data === "string") data = strToU8(data);
2508
2532
  if (!u8a2(data)) throw new Error("expected Uint8Array, got ".concat(typeof data === "undefined" ? "undefined" : _type_of(data)));
2509
2533
  return data;
2510
2534
  }
@@ -2918,7 +2942,7 @@ function compressPublicKeyHex(s) {
2918
2942
  return prefix + xHex;
2919
2943
  }
2920
2944
  function utf8ToHex(input) {
2921
- var bytes = utf8ToBytes(input);
2945
+ var bytes = strToU8(input);
2922
2946
  return bytesToHex(bytes);
2923
2947
  }
2924
2948
  function leftPad(input, num) {
@@ -3297,12 +3321,12 @@ var isLE2 = new Uint8Array(new Uint32Array([
3297
3321
  287454020
3298
3322
  ]).buffer)[0] === 68;
3299
3323
  if (!isLE2) throw new Error("Non little-endian hardware is not supported");
3300
- function utf8ToBytes3(str) {
3324
+ function utf8ToBytes2(str) {
3301
3325
  if (typeof str !== "string") throw new Error("string expected");
3302
3326
  return new Uint8Array(new TextEncoder().encode(str));
3303
3327
  }
3304
3328
  function toBytes2(data) {
3305
- if (typeof data === "string") data = utf8ToBytes3(data);
3329
+ if (typeof data === "string") data = utf8ToBytes2(data);
3306
3330
  else if (isBytes(data)) data = copyBytes(data);
3307
3331
  else throw new Error("Uint8Array expected, got " + (typeof data === "undefined" ? "undefined" : _type_of(data)));
3308
3332
  return data;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sm-crypto-v2",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "description": "sm-crypto-v2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",