SM4.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. !(function (r, e) {
  2. "object" == typeof exports && "object" == typeof module ?
  3. (module.exports = e()) :
  4. "function" == typeof define && define.amd ?
  5. define([], e) :
  6. "object" == typeof exports ?
  7. (exports.JSSM4 = e()) :
  8. (r.JSSM4 = e());
  9. })(this, function () {
  10. return (function (r) {
  11. var e = {};
  12. function t(n) {
  13. if (e[n]) return e[n].exports;
  14. var o = (e[n] = {
  15. i: n,
  16. l: !1,
  17. exports: {},
  18. });
  19. return r[n].call(o.exports, o, o.exports, t), (o.l = !0), o.exports;
  20. }
  21. return (
  22. (t.m = r),
  23. (t.c = e),
  24. (t.d = function (r, e, n) {
  25. t.o(r, e) ||
  26. Object.defineProperty(r, e, {
  27. enumerable: !0,
  28. get: n,
  29. });
  30. }),
  31. (t.r = function (r) {
  32. "undefined" != typeof Symbol &&
  33. Symbol.toStringTag &&
  34. Object.defineProperty(r, Symbol.toStringTag, {
  35. value: "Module",
  36. }),
  37. Object.defineProperty(r, "__esModule", {
  38. value: !0,
  39. });
  40. }),
  41. (t.t = function (r, e) {
  42. if ((1 & e && (r = t(r)), 8 & e)) return r;
  43. if (4 & e && "object" == typeof r && r && r.__esModule) return r;
  44. var n = Object.create(null);
  45. if (
  46. (t.r(n),
  47. Object.defineProperty(n, "default", {
  48. enumerable: !0,
  49. value: r,
  50. }),
  51. 2 & e && "string" != typeof r)
  52. )
  53. for (var o in r)
  54. t.d(
  55. n,
  56. o,
  57. function (e) {
  58. return r[e];
  59. }.bind(null, o)
  60. );
  61. return n;
  62. }),
  63. (t.n = function (r) {
  64. var e =
  65. r && r.__esModule ?
  66. function () {
  67. return r.default;
  68. } :
  69. function () {
  70. return r;
  71. };
  72. return t.d(e, "a", e), e;
  73. }),
  74. (t.o = function (r, e) {
  75. return Object.prototype.hasOwnProperty.call(r, e);
  76. }),
  77. (t.p = ""),
  78. t((t.s = 1))
  79. );
  80. })([
  81. function (r, e, t) {
  82. "use strict";
  83. (e.byteLength = function (r) {
  84. var e = c(r),
  85. t = e[0],
  86. n = e[1];
  87. return (3 * (t + n)) / 4 - n;
  88. }),
  89. (e.toByteArray = function (r) {
  90. var e,
  91. t,
  92. n = c(r),
  93. i = n[0],
  94. a = n[1],
  95. f = new u(
  96. (function (r, e, t) {
  97. return (3 * (e + t)) / 4 - t;
  98. })(0, i, a)
  99. ),
  100. l = 0,
  101. s = a > 0 ? i - 4 : i;
  102. for (t = 0; t < s; t += 4)
  103. (e =
  104. (o[r.charCodeAt(t)] << 18) |
  105. (o[r.charCodeAt(t + 1)] << 12) |
  106. (o[r.charCodeAt(t + 2)] << 6) |
  107. o[r.charCodeAt(t + 3)]),
  108. (f[l++] = (e >> 16) & 255),
  109. (f[l++] = (e >> 8) & 255),
  110. (f[l++] = 255 & e);
  111. 2 === a &&
  112. ((e = (o[r.charCodeAt(t)] << 2) | (o[r.charCodeAt(t + 1)] >> 4)),
  113. (f[l++] = 255 & e));
  114. 1 === a &&
  115. ((e =
  116. (o[r.charCodeAt(t)] << 10) |
  117. (o[r.charCodeAt(t + 1)] << 4) |
  118. (o[r.charCodeAt(t + 2)] >> 2)),
  119. (f[l++] = (e >> 8) & 255),
  120. (f[l++] = 255 & e));
  121. return f;
  122. }),
  123. (e.fromByteArray = function (r) {
  124. for (
  125. var e, t = r.length, o = t % 3, u = [], i = 0, a = t - o; i < a; i += 16383
  126. )
  127. u.push(l(r, i, i + 16383 > a ? a : i + 16383));
  128. 1 === o ?
  129. ((e = r[t - 1]), u.push(n[e >> 2] + n[(e << 4) & 63] + "==")) :
  130. 2 === o &&
  131. ((e = (r[t - 2] << 8) + r[t - 1]),
  132. u.push(n[e >> 10] + n[(e >> 4) & 63] + n[(e << 2) & 63] + "="));
  133. return u.join("");
  134. });
  135. for (
  136. var n = [],
  137. o = [],
  138. u = "undefined" != typeof Uint8Array ? Uint8Array : Array,
  139. i =
  140. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  141. a = 0,
  142. f = i.length; a < f;
  143. ++a
  144. )
  145. (n[a] = i[a]), (o[i.charCodeAt(a)] = a);
  146. function c(r) {
  147. var e = r.length;
  148. if (e % 4 > 0)
  149. throw new Error("Invalid string. Length must be a multiple of 4");
  150. var t = r.indexOf("=");
  151. return -1 === t && (t = e), [t, t === e ? 0 : 4 - (t % 4)];
  152. }
  153. function l(r, e, t) {
  154. for (var o, u, i = [], a = e; a < t; a += 3)
  155. (o =
  156. ((r[a] << 16) & 16711680) +
  157. ((r[a + 1] << 8) & 65280) +
  158. (255 & r[a + 2])),
  159. i.push(
  160. n[((u = o) >> 18) & 63] +
  161. n[(u >> 12) & 63] +
  162. n[(u >> 6) & 63] +
  163. n[63 & u]
  164. );
  165. return i.join("");
  166. }
  167. (o["-".charCodeAt(0)] = 62), (o["_".charCodeAt(0)] = 63);
  168. },
  169. function (r, e, t) {
  170. "use strict";
  171. t.r(e);
  172. var n = t(0),
  173. o = t.n(n);
  174. function u() {
  175. (this.mode = 1), (this.sk = new Array(32)), (this.isPadding = !0);
  176. }
  177. function i(r, e) {
  178. return (
  179. ((255 & r[e]) << 24) |
  180. ((255 & r[e + 1]) << 16) |
  181. ((255 & r[e + 2]) << 8) |
  182. (255 & r[e + 3])
  183. );
  184. }
  185. function a(r, e, t) {
  186. (e[t] = 255 & (r >> 24)),
  187. (e[t + 1] = 255 & (r >> 16)),
  188. (e[t + 2] = 255 & (r >> 8)),
  189. (e[t + 3] = 255 & r);
  190. }
  191. function f(r, e, t = 32) {
  192. return (r << (e %= t)) | (r >>> (t - e));
  193. }
  194. var c = [
  195. 214, 144, 233, 254, 204, 225, 61, 183, 22, 182, 20, 194, 40, 251, 44,
  196. 5, 43, 103, 154, 118, 42, 190, 4, 195, 170, 68, 19, 38, 73, 134, 6,
  197. 153, 156, 66, 80, 244, 145, 239, 152, 122, 51, 84, 11, 67, 237, 207,
  198. 172, 98, 228, 179, 28, 169, 201, 8, 232, 149, 128, 223, 148, 250, 117,
  199. 143, 63, 166, 71, 7, 167, 252, 243, 115, 23, 186, 131, 89, 60, 25,
  200. 230, 133, 79, 168, 104, 107, 129, 178, 113, 100, 218, 139, 248, 235,
  201. 15, 75, 112, 86, 157, 53, 30, 36, 14, 94, 99, 88, 209, 162, 37, 34,
  202. 124, 59, 1, 33, 120, 135, 212, 0, 70, 87, 159, 211, 39, 82, 76, 54, 2,
  203. 231, 160, 196, 200, 158, 234, 191, 138, 210, 64, 199, 56, 181, 163,
  204. 247, 242, 206, 249, 97, 21, 161, 224, 174, 93, 164, 155, 52, 26, 85,
  205. 173, 147, 50, 48, 245, 140, 177, 227, 29, 246, 226, 46, 130, 102, 202,
  206. 96, 192, 41, 35, 171, 13, 83, 78, 111, 213, 219, 55, 69, 222, 253,
  207. 142, 47, 3, 255, 106, 114, 109, 108, 91, 81, 141, 27, 175, 146, 187,
  208. 221, 188, 127, 17, 217, 92, 65, 31, 16, 90, 216, 10, 193, 49, 136,
  209. 165, 205, 123, 189, 45, 116, 208, 18, 184, 229, 180, 176, 137, 105,
  210. 151, 74, 12, 150, 119, 126, 101, 185, 241, 9, 197, 110, 198, 132, 24,
  211. 240, 125, 236, 58, 220, 77, 32, 121, 238, 95, 62, 215, 203, 57, 72,
  212. ],
  213. l = [
  214. 462357, 472066609, 943670861, 1415275113, 1886879365, 2358483617,
  215. 2830087869, 3301692121, 3773296373, 4228057617, 404694573, 876298825,
  216. 1347903077, 1819507329, 2291111581, 2762715833, 3234320085,
  217. 3705924337, 4177462797, 337322537, 808926789, 1280531041, 1752135293,
  218. 2223739545, 2695343797, 3166948049, 3638552301, 4110090761, 269950501,
  219. 741554753, 1213159005, 1684763257,
  220. ],
  221. s = [2746333894, 1453994832, 1736282519, 2993693404];
  222. function d(r) {
  223. return c[255 & r];
  224. }
  225. function h(r, e, t, n, o) {
  226. return (
  227. r ^
  228. ((u = e ^ t ^ n ^ o),
  229. (l = new Array(4)),
  230. (s = new Array(4)),
  231. a(u, l, 0),
  232. (s[0] = d(l[0])),
  233. (s[1] = d(l[1])),
  234. (s[2] = d(l[2])),
  235. (s[3] = d(l[3])),
  236. (c = i(s, 0)) ^ f(c, 2) ^ f(c, 10) ^ f(c, 18) ^ f(c, 24))
  237. );
  238. var u, c, l, s;
  239. }
  240. function y(r, e) {
  241. var t,
  242. n,
  243. o,
  244. u,
  245. c = new Array(4),
  246. h = new Array(36),
  247. y = 0;
  248. for (
  249. c[0] = i(e, 0),
  250. c[1] = i(e, 4),
  251. c[2] = i(e, 8),
  252. c[3] = i(e, 12),
  253. h[0] = c[0] ^ s[0],
  254. h[1] = c[1] ^ s[1],
  255. h[2] = c[2] ^ s[2],
  256. h[3] = c[3] ^ s[3]; y < 32; y++
  257. )
  258. (h[y + 4] =
  259. h[y] ^
  260. ((t = h[y + 1] ^ h[y + 2] ^ h[y + 3] ^ l[y]),
  261. (n = void 0),
  262. (o = void 0),
  263. (u = void 0),
  264. (o = new Array(4)),
  265. (u = new Array(4)),
  266. a(t, o, 0),
  267. (u[0] = d(o[0])),
  268. (u[1] = d(o[1])),
  269. (u[2] = d(o[2])),
  270. (u[3] = d(o[3])),
  271. (n = i(u, 0)) ^ f(n, 13) ^ f(n, 23))),
  272. (r[y] = h[y + 4]);
  273. }
  274. function p(r, e, t) {
  275. var n = 0,
  276. o = new Array(36);
  277. for (
  278. o[0] = i(e, 0), o[1] = i(e, 4), o[2] = i(e, 8), o[3] = i(e, 12); n < 32;
  279. )
  280. (o[n + 4] = h(o[n], o[n + 1], o[n + 2], o[n + 3], r[n])), n++;
  281. a(o[35], t, 0), a(o[34], t, 4), a(o[33], t, 8), a(o[32], t, 12);
  282. }
  283. function v(r, e) {
  284. if (null == r) return null;
  285. var t = [];
  286. if (((t = t.concat(r)), 1 == e))
  287. for (var n = 16 - (r.length % 16), o = 0; o < n; o++)
  288. t[r.length + o] = n;
  289. else {
  290. n = r[r.length - 1];
  291. for (var u = 0; u < n; u++) t.pop();
  292. }
  293. return t;
  294. }
  295. function g(r, e) {
  296. (null == e) | ("" == e) && Error("input is null!"),
  297. r.isPadding && 1 == r.mode && (e = v(e, 1));
  298. for (var t = e.length, n = [], o = 0; o < t; o += 16) {
  299. var u = [],
  300. i = [];
  301. u.push(
  302. e[o + 0],
  303. e[o + 1],
  304. e[o + 2],
  305. e[o + 3],
  306. e[o + 4],
  307. e[o + 5],
  308. e[o + 6],
  309. e[o + 7],
  310. e[o + 8],
  311. e[o + 9],
  312. e[o + 10],
  313. e[o + 11],
  314. e[o + 12],
  315. e[o + 13],
  316. e[o + 14],
  317. e[o + 15]
  318. ),
  319. p(r.sk, u, i),
  320. (n = n.concat(i));
  321. }
  322. return r.isPadding && 0 == r.mode && (n = v(n, 0)), n;
  323. }
  324. function A(r) {
  325. var e,
  326. t,
  327. n = new Array();
  328. e = r.length;
  329. for (var o = 0; o < e; o++)
  330. (t = r.charCodeAt(o)) >= 65536 && t <= 1114111 ?
  331. (n.push(((t >> 18) & 7) | 240),
  332. n.push(((t >> 12) & 63) | 128),
  333. n.push(((t >> 6) & 63) | 128),
  334. n.push((63 & t) | 128)) :
  335. t >= 2048 && t <= 65535 ?
  336. (n.push(((t >> 12) & 15) | 224),
  337. n.push(((t >> 6) & 63) | 128),
  338. n.push((63 & t) | 128)) :
  339. t >= 128 && t <= 2047 ?
  340. (n.push(((t >> 6) & 31) | 192), n.push((63 & t) | 128)) :
  341. n.push(255 & t);
  342. return n;
  343. }
  344. e.default = function (r) {
  345. (this.seckey = r),
  346. (this.encryptData_ECB = function (r) {
  347. var e,
  348. t = new u();
  349. (t.isPadding = !0), (t.mode = 1);
  350. try {
  351. if (null == this.seckey) throw "key 不规范";
  352. e = A(this.seckey);
  353. } catch (r) {
  354. Error(r.message);
  355. }!(function (r, e) {
  356. null == r && Error("ctx is null!"),
  357. (null != e && 16 == e.length) || Error("key error!"),
  358. (r.mode = 1),
  359. y(r.sk, e);
  360. })(t, e);
  361. var n = g(t, A(r)),
  362. i = o.a.fromByteArray(n);
  363. null != i &&
  364. i.trim().length > 0 &&
  365. i.replace(/(\s*|\t|\r|\n)/g, "");
  366. return i;
  367. }),
  368. (this.decryptData_ECB = function (r) {
  369. try {
  370. var e = new u();
  371. (e.isPadding = !0), (e.mode = 0);
  372. var t = A(this.seckey);
  373. return (
  374. (function (r, e) {
  375. null == r && Error("ctx is null!"),
  376. (null != e && 16 == e.length) || Error("key error!"),
  377. (r.mode = 0),
  378. y(r.sk, e),
  379. (r.sk = r.sk.reverse());
  380. })(e, t),
  381. (function (r) {
  382. if ("string" == typeof r) return r;
  383. for (var e = "", t = r, n = 0; n < t.length; n++) {
  384. var o = t[n].toString(2),
  385. u = o.match(/^1+?(?=0)/);
  386. if (u && 8 == o.length) {
  387. for (
  388. var i = u[0].length,
  389. a = t[n].toString(2).slice(7 - i),
  390. f = 1; f < i; f++
  391. )
  392. a += t[f + n].toString(2).slice(2);
  393. (e += String.fromCharCode(parseInt(a, 2))), (n += i - 1);
  394. } else e += String.fromCharCode(t[n]);
  395. }
  396. return e;
  397. })(g(e, o.a.toByteArray(r)))
  398. );
  399. } catch (r) {
  400. return Error(r.message), null;
  401. }
  402. });
  403. };
  404. },
  405. ]).default;
  406. });