index.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  1. module.exports =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/
  6. /******/ // The require function
  7. /******/ function __webpack_require__(moduleId) {
  8. /******/
  9. /******/ // Check if module is in cache
  10. /******/ if(installedModules[moduleId]) {
  11. /******/ return installedModules[moduleId].exports;
  12. /******/ }
  13. /******/ // Create a new module (and put it into the cache)
  14. /******/ var module = installedModules[moduleId] = {
  15. /******/ i: moduleId,
  16. /******/ l: false,
  17. /******/ exports: {}
  18. /******/ };
  19. /******/
  20. /******/ // Execute the module function
  21. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. /******/
  23. /******/ // Flag the module as loaded
  24. /******/ module.l = true;
  25. /******/
  26. /******/ // Return the exports of the module
  27. /******/ return module.exports;
  28. /******/ }
  29. /******/
  30. /******/
  31. /******/ // expose the modules object (__webpack_modules__)
  32. /******/ __webpack_require__.m = modules;
  33. /******/
  34. /******/ // expose the module cache
  35. /******/ __webpack_require__.c = installedModules;
  36. /******/
  37. /******/ // define getter function for harmony exports
  38. /******/ __webpack_require__.d = function(exports, name, getter) {
  39. /******/ if(!__webpack_require__.o(exports, name)) {
  40. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41. /******/ }
  42. /******/ };
  43. /******/
  44. /******/ // define __esModule on exports
  45. /******/ __webpack_require__.r = function(exports) {
  46. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48. /******/ }
  49. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  50. /******/ };
  51. /******/
  52. /******/ // create a fake namespace object
  53. /******/ // mode & 1: value is a module id, require it
  54. /******/ // mode & 2: merge all properties of value into the ns
  55. /******/ // mode & 4: return value when already ns object
  56. /******/ // mode & 8|1: behave like require
  57. /******/ __webpack_require__.t = function(value, mode) {
  58. /******/ if(mode & 1) value = __webpack_require__(value);
  59. /******/ if(mode & 8) return value;
  60. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61. /******/ var ns = Object.create(null);
  62. /******/ __webpack_require__.r(ns);
  63. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65. /******/ return ns;
  66. /******/ };
  67. /******/
  68. /******/ // getDefaultExport function for compatibility with non-harmony modules
  69. /******/ __webpack_require__.n = function(module) {
  70. /******/ var getter = module && module.__esModule ?
  71. /******/ function getDefault() { return module['default']; } :
  72. /******/ function getModuleExports() { return module; };
  73. /******/ __webpack_require__.d(getter, 'a', getter);
  74. /******/ return getter;
  75. /******/ };
  76. /******/
  77. /******/ // Object.prototype.hasOwnProperty.call
  78. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79. /******/
  80. /******/ // __webpack_public_path__
  81. /******/ __webpack_require__.p = "";
  82. /******/
  83. /******/
  84. /******/ // Load entry module and return exports
  85. /******/ return __webpack_require__(__webpack_require__.s = 3);
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ([
  89. /* 0 */
  90. /***/ (function(module, exports) {
  91. module.exports = require("jsbn");
  92. /***/ }),
  93. /* 1 */
  94. /***/ (function(module, exports, __webpack_require__) {
  95. "use strict";
  96. /* eslint-disable no-bitwise, no-mixed-operators, no-use-before-define, max-len */
  97. var _require = __webpack_require__(0),
  98. BigInteger = _require.BigInteger,
  99. SecureRandom = _require.SecureRandom;
  100. var _require2 = __webpack_require__(6),
  101. ECCurveFp = _require2.ECCurveFp;
  102. var rng = new SecureRandom();
  103. var _generateEcparam = generateEcparam(),
  104. curve = _generateEcparam.curve,
  105. G = _generateEcparam.G,
  106. n = _generateEcparam.n;
  107. /**
  108. * 获取公共椭圆曲线
  109. */
  110. function getGlobalCurve() {
  111. return curve;
  112. }
  113. /**
  114. * 生成ecparam
  115. */
  116. function generateEcparam() {
  117. // 椭圆曲线
  118. var p = new BigInteger('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF', 16);
  119. var a = new BigInteger('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC', 16);
  120. var b = new BigInteger('28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93', 16);
  121. var curve = new ECCurveFp(p, a, b);
  122. // 基点
  123. var gxHex = '32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7';
  124. var gyHex = 'BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0';
  125. var G = curve.decodePointHex('04' + gxHex + gyHex);
  126. var n = new BigInteger('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123', 16);
  127. return { curve: curve, G: G, n: n };
  128. }
  129. /**
  130. * 生成密钥对
  131. */
  132. function generateKeyPairHex() {
  133. var d = new BigInteger(n.bitLength(), rng).mod(n.subtract(BigInteger.ONE)).add(BigInteger.ONE); // 随机数
  134. var privateKey = leftPad(d.toString(16), 64);
  135. var P = G.multiply(d); // P = dG,p 为公钥,d 为私钥
  136. var Px = leftPad(P.getX().toBigInteger().toString(16), 64);
  137. var Py = leftPad(P.getY().toBigInteger().toString(16), 64);
  138. var publicKey = '04' + Px + Py;
  139. return { privateKey: privateKey, publicKey: publicKey };
  140. }
  141. /**
  142. * 解析utf8字符串到16进制
  143. */
  144. function parseUtf8StringToHex(input) {
  145. input = unescape(encodeURIComponent(input));
  146. var length = input.length;
  147. // 转换到字数组
  148. var words = [];
  149. for (var i = 0; i < length; i++) {
  150. words[i >>> 2] |= (input.charCodeAt(i) & 0xff) << 24 - i % 4 * 8;
  151. }
  152. // 转换到16进制
  153. var hexChars = [];
  154. for (var _i = 0; _i < length; _i++) {
  155. var bite = words[_i >>> 2] >>> 24 - _i % 4 * 8 & 0xff;
  156. hexChars.push((bite >>> 4).toString(16));
  157. hexChars.push((bite & 0x0f).toString(16));
  158. }
  159. return hexChars.join('');
  160. }
  161. /**
  162. * 解析arrayBuffer到16进制字符串
  163. */
  164. function parseArrayBufferToHex(input) {
  165. return Array.prototype.map.call(new Uint8Array(input), function (x) {
  166. return ('00' + x.toString(16)).slice(-2);
  167. }).join('');
  168. }
  169. /**
  170. * 补全16进制字符串
  171. */
  172. function leftPad(input, num) {
  173. if (input.length >= num) return input;
  174. return new Array(num - input.length + 1).join('0') + input;
  175. }
  176. /**
  177. * 转成16进制串
  178. */
  179. function arrayToHex(arr) {
  180. var words = [];
  181. var j = 0;
  182. for (var i = 0; i < arr.length * 2; i += 2) {
  183. words[i >>> 3] |= parseInt(arr[j], 10) << 24 - i % 8 * 4;
  184. j++;
  185. }
  186. // 转换到16进制
  187. var hexChars = [];
  188. for (var _i2 = 0; _i2 < arr.length; _i2++) {
  189. var bite = words[_i2 >>> 2] >>> 24 - _i2 % 4 * 8 & 0xff;
  190. hexChars.push((bite >>> 4).toString(16));
  191. hexChars.push((bite & 0x0f).toString(16));
  192. }
  193. return hexChars.join('');
  194. }
  195. /**
  196. * 转成utf8串
  197. */
  198. function arrayToUtf8(arr) {
  199. var words = [];
  200. var j = 0;
  201. for (var i = 0; i < arr.length * 2; i += 2) {
  202. words[i >>> 3] |= parseInt(arr[j], 10) << 24 - i % 8 * 4;
  203. j++;
  204. }
  205. try {
  206. var latin1Chars = [];
  207. for (var _i3 = 0; _i3 < arr.length; _i3++) {
  208. var bite = words[_i3 >>> 2] >>> 24 - _i3 % 4 * 8 & 0xff;
  209. latin1Chars.push(String.fromCharCode(bite));
  210. }
  211. return decodeURIComponent(escape(latin1Chars.join('')));
  212. } catch (e) {
  213. throw new Error('Malformed UTF-8 data');
  214. }
  215. }
  216. /**
  217. * 转成ascii码数组
  218. */
  219. function hexToArray(hexStr) {
  220. var words = [];
  221. var hexStrLength = hexStr.length;
  222. if (hexStrLength % 2 !== 0) {
  223. hexStr = leftPad(hexStr, hexStrLength + 1);
  224. }
  225. hexStrLength = hexStr.length;
  226. for (var i = 0; i < hexStrLength; i += 2) {
  227. words.push(parseInt(hexStr.substr(i, 2), 16));
  228. }
  229. return words;
  230. }
  231. module.exports = {
  232. getGlobalCurve: getGlobalCurve,
  233. generateEcparam: generateEcparam,
  234. generateKeyPairHex: generateKeyPairHex,
  235. parseUtf8StringToHex: parseUtf8StringToHex,
  236. parseArrayBufferToHex: parseArrayBufferToHex,
  237. leftPad: leftPad,
  238. arrayToHex: arrayToHex,
  239. arrayToUtf8: arrayToUtf8,
  240. hexToArray: hexToArray
  241. };
  242. /***/ }),
  243. /* 2 */
  244. /***/ (function(module, exports, __webpack_require__) {
  245. "use strict";
  246. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  247. /* eslint-disable no-bitwise, no-mixed-operators, class-methods-use-this, camelcase */
  248. var _require = __webpack_require__(0),
  249. BigInteger = _require.BigInteger;
  250. var _ = __webpack_require__(1);
  251. var copyArray = function copyArray(sourceArray, sourceIndex, destinationArray, destinationIndex, length) {
  252. for (var i = 0; i < length; i++) {
  253. destinationArray[destinationIndex + i] = sourceArray[sourceIndex + i];
  254. }
  255. };
  256. var Int32 = {
  257. minValue: -2147483648,
  258. maxValue: 2147483647,
  259. parse: function parse(n) {
  260. if (n < this.minValue) {
  261. var bigInteger = Number(-n);
  262. var bigIntegerRadix = bigInteger.toString(2);
  263. var subBigIntegerRadix = bigIntegerRadix.substr(bigIntegerRadix.length - 31, 31);
  264. var reBigIntegerRadix = '';
  265. for (var i = 0; i < subBigIntegerRadix.length; i++) {
  266. var subBigIntegerRadixItem = subBigIntegerRadix.substr(i, 1);
  267. reBigIntegerRadix += subBigIntegerRadixItem === '0' ? '1' : '0';
  268. }
  269. var result = parseInt(reBigIntegerRadix, 2);
  270. return result + 1;
  271. } else if (n > this.maxValue) {
  272. var _bigInteger = Number(n);
  273. var _bigIntegerRadix = _bigInteger.toString(2);
  274. var _subBigIntegerRadix = _bigIntegerRadix.substr(_bigIntegerRadix.length - 31, 31);
  275. var _reBigIntegerRadix = '';
  276. for (var _i = 0; _i < _subBigIntegerRadix.length; _i++) {
  277. var _subBigIntegerRadixItem = _subBigIntegerRadix.substr(_i, 1);
  278. _reBigIntegerRadix += _subBigIntegerRadixItem === '0' ? '1' : '0';
  279. }
  280. var _result = parseInt(_reBigIntegerRadix, 2);
  281. return -(_result + 1);
  282. } else {
  283. return n;
  284. }
  285. },
  286. parseByte: function parseByte(n) {
  287. if (n < 0) {
  288. var bigInteger = Number(-n);
  289. var bigIntegerRadix = bigInteger.toString(2);
  290. var subBigIntegerRadix = bigIntegerRadix.substr(bigIntegerRadix.length - 8, 8);
  291. var reBigIntegerRadix = '';
  292. for (var i = 0; i < subBigIntegerRadix.length; i++) {
  293. var subBigIntegerRadixItem = subBigIntegerRadix.substr(i, 1);
  294. reBigIntegerRadix += subBigIntegerRadixItem === '0' ? '1' : '0';
  295. }
  296. var result = parseInt(reBigIntegerRadix, 2);
  297. return result + 1;
  298. } else if (n > 255) {
  299. var _bigInteger2 = Number(n);
  300. var _bigIntegerRadix2 = _bigInteger2.toString(2);
  301. return parseInt(_bigIntegerRadix2.substr(_bigIntegerRadix2.length - 8, 8), 2);
  302. } else {
  303. return n;
  304. }
  305. }
  306. };
  307. var SM3Digest = function () {
  308. function SM3Digest() {
  309. _classCallCheck(this, SM3Digest);
  310. this.xBuf = [];
  311. this.xBufOff = 0;
  312. this.byteCount = 0;
  313. this.DIGEST_LENGTH = 32;
  314. this.v0 = [0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600, 0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e];
  315. this.v0 = [0x7380166f, 0x4914b2b9, 0x172442d7, -628488704, -1452330820, 0x163138aa, -477237683, -1325724082];
  316. this.v = new Array(8);
  317. this.v_ = new Array(8);
  318. this.X0 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  319. this.X = new Array(68);
  320. this.xOff = 0;
  321. this.T_00_15 = 0x79cc4519;
  322. this.T_16_63 = 0x7a879d8a;
  323. if (arguments.length > 0) {
  324. this.initDigest(arguments.length <= 0 ? undefined : arguments[0]);
  325. } else {
  326. this.init();
  327. }
  328. }
  329. SM3Digest.prototype.init = function init() {
  330. this.xBuf = new Array(4);
  331. this.reset();
  332. };
  333. SM3Digest.prototype.initDigest = function initDigest(t) {
  334. this.xBuf = [].concat(t.xBuf);
  335. this.xBufOff = t.xBufOff;
  336. this.byteCount = t.byteCount;
  337. copyArray(t.X, 0, this.X, 0, t.X.length);
  338. this.xOff = t.xOff;
  339. copyArray(t.v, 0, this.v, 0, t.v.length);
  340. };
  341. SM3Digest.prototype.getDigestSize = function getDigestSize() {
  342. return this.DIGEST_LENGTH;
  343. };
  344. SM3Digest.prototype.reset = function reset() {
  345. this.byteCount = 0;
  346. this.xBufOff = 0;
  347. var keys = Object.keys(this.xBuf);
  348. for (var i = 0, len = keys.length; i < len; i++) {
  349. this.xBuf[keys[i]] = null;
  350. }copyArray(this.v0, 0, this.v, 0, this.v0.length);
  351. this.xOff = 0;
  352. copyArray(this.X0, 0, this.X, 0, this.X0.length);
  353. };
  354. SM3Digest.prototype.processBlock = function processBlock() {
  355. var i = void 0;
  356. var ww = this.X;
  357. var ww_ = new Array(64);
  358. for (i = 16; i < 68; i++) {
  359. ww[i] = this.p1(ww[i - 16] ^ ww[i - 9] ^ this.rotate(ww[i - 3], 15)) ^ this.rotate(ww[i - 13], 7) ^ ww[i - 6];
  360. }
  361. for (i = 0; i < 64; i++) {
  362. ww_[i] = ww[i] ^ ww[i + 4];
  363. }
  364. var vv = this.v;
  365. var vv_ = this.v_;
  366. copyArray(vv, 0, vv_, 0, this.v0.length);
  367. var SS1 = void 0;var SS2 = void 0;var TT1 = void 0;var TT2 = void 0;var aaa = void 0;
  368. for (i = 0; i < 16; i++) {
  369. aaa = this.rotate(vv_[0], 12);
  370. SS1 = Int32.parse(Int32.parse(aaa + vv_[4]) + this.rotate(this.T_00_15, i));
  371. SS1 = this.rotate(SS1, 7);
  372. SS2 = SS1 ^ aaa;
  373. TT1 = Int32.parse(Int32.parse(this.ff_00_15(vv_[0], vv_[1], vv_[2]) + vv_[3]) + SS2) + ww_[i];
  374. TT2 = Int32.parse(Int32.parse(this.gg_00_15(vv_[4], vv_[5], vv_[6]) + vv_[7]) + SS1) + ww[i];
  375. vv_[3] = vv_[2];
  376. vv_[2] = this.rotate(vv_[1], 9);
  377. vv_[1] = vv_[0];
  378. vv_[0] = TT1;
  379. vv_[7] = vv_[6];
  380. vv_[6] = this.rotate(vv_[5], 19);
  381. vv_[5] = vv_[4];
  382. vv_[4] = this.p0(TT2);
  383. }
  384. for (i = 16; i < 64; i++) {
  385. aaa = this.rotate(vv_[0], 12);
  386. SS1 = Int32.parse(Int32.parse(aaa + vv_[4]) + this.rotate(this.T_16_63, i));
  387. SS1 = this.rotate(SS1, 7);
  388. SS2 = SS1 ^ aaa;
  389. TT1 = Int32.parse(Int32.parse(this.ff_16_63(vv_[0], vv_[1], vv_[2]) + vv_[3]) + SS2) + ww_[i];
  390. TT2 = Int32.parse(Int32.parse(this.gg_16_63(vv_[4], vv_[5], vv_[6]) + vv_[7]) + SS1) + ww[i];
  391. vv_[3] = vv_[2];
  392. vv_[2] = this.rotate(vv_[1], 9);
  393. vv_[1] = vv_[0];
  394. vv_[0] = TT1;
  395. vv_[7] = vv_[6];
  396. vv_[6] = this.rotate(vv_[5], 19);
  397. vv_[5] = vv_[4];
  398. vv_[4] = this.p0(TT2);
  399. }
  400. for (i = 0; i < 8; i++) {
  401. vv[i] ^= Int32.parse(vv_[i]);
  402. }
  403. this.xOff = 0;
  404. copyArray(this.X0, 0, this.X, 0, this.X0.length);
  405. };
  406. SM3Digest.prototype.processWord = function processWord(in_Renamed, inOff) {
  407. var n = in_Renamed[inOff] << 24;
  408. n |= (in_Renamed[++inOff] & 0xff) << 16;
  409. n |= (in_Renamed[++inOff] & 0xff) << 8;
  410. n |= in_Renamed[++inOff] & 0xff;
  411. this.X[this.xOff] = n;
  412. if (++this.xOff === 16) {
  413. this.processBlock();
  414. }
  415. };
  416. SM3Digest.prototype.processLength = function processLength(bitLength) {
  417. if (this.xOff > 14) {
  418. this.processBlock();
  419. }
  420. this.X[14] = this.urShiftLong(bitLength, 32);
  421. this.X[15] = bitLength & 0xffffffff;
  422. };
  423. SM3Digest.prototype.intToBigEndian = function intToBigEndian(n, bs, off) {
  424. bs[off] = Int32.parseByte(this.urShift(n, 24));
  425. bs[++off] = Int32.parseByte(this.urShift(n, 16));
  426. bs[++off] = Int32.parseByte(this.urShift(n, 8));
  427. bs[++off] = Int32.parseByte(n);
  428. };
  429. SM3Digest.prototype.doFinal = function doFinal(out_Renamed, outOff) {
  430. this.finish();
  431. for (var i = 0; i < 8; i++) {
  432. this.intToBigEndian(this.v[i], out_Renamed, outOff + i * 4);
  433. }
  434. this.reset();
  435. return this.DIGEST_LENGTH;
  436. };
  437. SM3Digest.prototype.update = function update(input) {
  438. this.xBuf[this.xBufOff++] = input;
  439. if (this.xBufOff === this.xBuf.length) {
  440. this.processWord(this.xBuf, 0);
  441. this.xBufOff = 0;
  442. }
  443. this.byteCount++;
  444. };
  445. SM3Digest.prototype.blockUpdate = function blockUpdate(input, inOff, length) {
  446. while (this.xBufOff !== 0 && length > 0) {
  447. this.update(input[inOff]);
  448. inOff++;
  449. length--;
  450. }
  451. while (length > this.xBuf.length) {
  452. this.processWord(input, inOff);
  453. inOff += this.xBuf.length;
  454. length -= this.xBuf.length;
  455. this.byteCount += this.xBuf.length;
  456. }
  457. while (length > 0) {
  458. this.update(input[inOff]);
  459. inOff++;
  460. length--;
  461. }
  462. };
  463. SM3Digest.prototype.finish = function finish() {
  464. var bitLength = this.byteCount << 3;
  465. this.update(128);
  466. while (this.xBufOff !== 0) {
  467. this.update(0);
  468. }this.processLength(bitLength);
  469. this.processBlock();
  470. };
  471. SM3Digest.prototype.rotate = function rotate(x, n) {
  472. return x << n | this.urShift(x, 32 - n);
  473. };
  474. SM3Digest.prototype.p0 = function p0(X) {
  475. return X ^ this.rotate(X, 9) ^ this.rotate(X, 17);
  476. };
  477. SM3Digest.prototype.p1 = function p1(X) {
  478. return X ^ this.rotate(X, 15) ^ this.rotate(X, 23);
  479. };
  480. SM3Digest.prototype.ff_00_15 = function ff_00_15(X, Y, Z) {
  481. return X ^ Y ^ Z;
  482. };
  483. SM3Digest.prototype.ff_16_63 = function ff_16_63(X, Y, Z) {
  484. return X & Y | X & Z | Y & Z;
  485. };
  486. SM3Digest.prototype.gg_00_15 = function gg_00_15(X, Y, Z) {
  487. return X ^ Y ^ Z;
  488. };
  489. SM3Digest.prototype.gg_16_63 = function gg_16_63(X, Y, Z) {
  490. return X & Y | ~X & Z;
  491. };
  492. SM3Digest.prototype.urShift = function urShift(number, bits) {
  493. if (number > Int32.maxValue || number < Int32.minValue) {
  494. number = Int32.parse(number);
  495. }
  496. if (number >= 0) {
  497. return number >> bits;
  498. } else {
  499. return (number >> bits) + (2 << ~bits);
  500. }
  501. };
  502. SM3Digest.prototype.urShiftLong = function urShiftLong(number, bits) {
  503. var returnV = void 0;
  504. var big = new BigInteger();
  505. big.fromInt(number);
  506. if (big.signum() >= 0) {
  507. returnV = big.shiftRight(bits).intValue();
  508. } else {
  509. var bigAdd = new BigInteger();
  510. bigAdd.fromInt(2);
  511. var shiftLeftBits = ~bits;
  512. var shiftLeftNumber = '';
  513. if (shiftLeftBits < 0) {
  514. var shiftRightBits = 64 + shiftLeftBits;
  515. for (var i = 0; i < shiftRightBits; i++) {
  516. shiftLeftNumber += '0';
  517. }
  518. var shiftLeftNumberBigAdd = new BigInteger();
  519. shiftLeftNumberBigAdd.fromInt(number >> bits);
  520. var shiftLeftNumberBig = new BigInteger('10' + shiftLeftNumber, 2);
  521. shiftLeftNumber = shiftLeftNumberBig.toRadix(10);
  522. var r = shiftLeftNumberBig.add(shiftLeftNumberBigAdd);
  523. returnV = r.toRadix(10);
  524. } else {
  525. shiftLeftNumber = bigAdd.shiftLeft(~bits).intValue();
  526. returnV = (number >> bits) + shiftLeftNumber;
  527. }
  528. }
  529. return returnV;
  530. };
  531. SM3Digest.prototype.getZ = function getZ(g, publicKey) {
  532. var userId = _.parseUtf8StringToHex('1234567812345678');
  533. var len = userId.length * 4;
  534. this.update(len >> 8 & 0x00ff);
  535. this.update(len & 0x00ff);
  536. var userIdWords = _.hexToArray(userId);
  537. this.blockUpdate(userIdWords, 0, userIdWords.length);
  538. var aWords = _.hexToArray(g.curve.a.toBigInteger().toRadix(16));
  539. var bWords = _.hexToArray(g.curve.b.toBigInteger().toRadix(16));
  540. var gxWords = _.hexToArray(g.getX().toBigInteger().toRadix(16));
  541. var gyWords = _.hexToArray(g.getY().toBigInteger().toRadix(16));
  542. var pxWords = _.hexToArray(publicKey.substr(0, 64));
  543. var pyWords = _.hexToArray(publicKey.substr(64, 64));
  544. this.blockUpdate(aWords, 0, aWords.length);
  545. this.blockUpdate(bWords, 0, bWords.length);
  546. this.blockUpdate(gxWords, 0, gxWords.length);
  547. this.blockUpdate(gyWords, 0, gyWords.length);
  548. this.blockUpdate(pxWords, 0, pxWords.length);
  549. this.blockUpdate(pyWords, 0, pyWords.length);
  550. var md = new Array(this.getDigestSize());
  551. this.doFinal(md, 0);
  552. return md;
  553. };
  554. return SM3Digest;
  555. }();
  556. module.exports = SM3Digest;
  557. /***/ }),
  558. /* 3 */
  559. /***/ (function(module, exports, __webpack_require__) {
  560. "use strict";
  561. module.exports = {
  562. sm2: __webpack_require__(4),
  563. sm3: __webpack_require__(8),
  564. sm4: __webpack_require__(9)
  565. };
  566. /***/ }),
  567. /* 4 */
  568. /***/ (function(module, exports, __webpack_require__) {
  569. "use strict";
  570. /* eslint-disable no-use-before-define */
  571. var _require = __webpack_require__(0),
  572. BigInteger = _require.BigInteger;
  573. var _require2 = __webpack_require__(5),
  574. encodeDer = _require2.encodeDer,
  575. decodeDer = _require2.decodeDer;
  576. var SM3Digest = __webpack_require__(2);
  577. var SM2Cipher = __webpack_require__(7);
  578. var _ = __webpack_require__(1);
  579. var _$generateEcparam = _.generateEcparam(),
  580. G = _$generateEcparam.G,
  581. curve = _$generateEcparam.curve,
  582. n = _$generateEcparam.n;
  583. var C1C2C3 = 0;
  584. /**
  585. * 加密
  586. */
  587. function doEncrypt(msg, publicKey) {
  588. var cipherMode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  589. var cipher = new SM2Cipher();
  590. msg = _.hexToArray(_.parseUtf8StringToHex(msg));
  591. if (publicKey.length > 128) {
  592. publicKey = publicKey.substr(publicKey.length - 128);
  593. }
  594. var xHex = publicKey.substr(0, 64);
  595. var yHex = publicKey.substr(64);
  596. publicKey = cipher.createPoint(xHex, yHex);
  597. var c1 = cipher.initEncipher(publicKey);
  598. cipher.encryptBlock(msg);
  599. var c2 = _.arrayToHex(msg);
  600. var c3 = new Array(32);
  601. cipher.doFinal(c3);
  602. c3 = _.arrayToHex(c3);
  603. return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
  604. }
  605. /**
  606. * 解密
  607. */
  608. function doDecrypt(encryptData, privateKey) {
  609. var cipherMode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  610. var cipher = new SM2Cipher();
  611. privateKey = new BigInteger(privateKey, 16);
  612. var c1X = encryptData.substr(0, 64);
  613. var c1Y = encryptData.substr(0 + c1X.length, 64);
  614. var c1Length = c1X.length + c1Y.length;
  615. var c3 = encryptData.substr(c1Length, 64);
  616. var c2 = encryptData.substr(c1Length + 64);
  617. if (cipherMode === C1C2C3) {
  618. c3 = encryptData.substr(encryptData.length - 64);
  619. c2 = encryptData.substr(c1Length, encryptData.length - c1Length - 64);
  620. }
  621. var data = _.hexToArray(c2);
  622. var c1 = cipher.createPoint(c1X, c1Y);
  623. cipher.initDecipher(privateKey, c1);
  624. cipher.decryptBlock(data);
  625. var c3_ = new Array(32);
  626. cipher.doFinal(c3_);
  627. var isDecrypt = _.arrayToHex(c3_) === c3;
  628. if (isDecrypt) {
  629. var decryptData = _.arrayToUtf8(data);
  630. return decryptData;
  631. } else {
  632. return '';
  633. }
  634. }
  635. /**
  636. * 签名
  637. */
  638. function doSignature(msg, privateKey) {
  639. var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
  640. pointPool = _ref.pointPool,
  641. der = _ref.der,
  642. hash = _ref.hash,
  643. publicKey = _ref.publicKey;
  644. var hashHex = typeof msg === 'string' ? _.parseUtf8StringToHex(msg) : _.parseArrayBufferToHex(msg);
  645. if (hash) {
  646. // sm3杂凑
  647. publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey);
  648. hashHex = doSm3Hash(hashHex, publicKey);
  649. }
  650. var dA = new BigInteger(privateKey, 16);
  651. var e = new BigInteger(hashHex, 16);
  652. // k
  653. var k = null;
  654. var r = null;
  655. var s = null;
  656. do {
  657. do {
  658. var point = void 0;
  659. if (pointPool && pointPool.length) {
  660. point = pointPool.pop();
  661. } else {
  662. point = getPoint();
  663. }
  664. k = point.k;
  665. // r = (e + x1) mod n
  666. r = e.add(point.x1).mod(n);
  667. } while (r.equals(BigInteger.ZERO) || r.add(k).equals(n));
  668. // s = ((1 + dA)^-1 * (k - r * dA)) mod n
  669. s = dA.add(BigInteger.ONE).modInverse(n).multiply(k.subtract(r.multiply(dA))).mod(n);
  670. } while (s.equals(BigInteger.ZERO));
  671. if (der) {
  672. // asn1 der编码
  673. return encodeDer(r, s);
  674. }
  675. return _.leftPad(r.toString(16), 64) + _.leftPad(s.toString(16), 64);
  676. }
  677. /**
  678. * 验签
  679. */
  680. function doVerifySignature(msg, signHex, publicKey) {
  681. var _ref2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
  682. der = _ref2.der,
  683. hash = _ref2.hash;
  684. var hashHex = typeof msg === 'string' ? _.parseUtf8StringToHex(msg) : _.parseArrayBufferToHex(msg);
  685. if (hash) {
  686. // sm3杂凑
  687. hashHex = doSm3Hash(hashHex, publicKey);
  688. }
  689. var r = void 0;var s = void 0;
  690. if (der) {
  691. var decodeDerObj = decodeDer(signHex);
  692. r = decodeDerObj.r;
  693. s = decodeDerObj.s;
  694. } else {
  695. r = new BigInteger(signHex.substring(0, 64), 16);
  696. s = new BigInteger(signHex.substring(64), 16);
  697. }
  698. var PA = curve.decodePointHex(publicKey);
  699. var e = new BigInteger(hashHex, 16);
  700. // t = (r + s) mod n
  701. var t = r.add(s).mod(n);
  702. if (t.equals(BigInteger.ZERO)) return false;
  703. // x1y1 = s * G + t * PA
  704. var x1y1 = G.multiply(s).add(PA.multiply(t));
  705. // R = (e + x1) mod n
  706. var R = e.add(x1y1.getX().toBigInteger()).mod(n);
  707. return r.equals(R);
  708. }
  709. /**
  710. * sm3杂凑算法
  711. */
  712. function doSm3Hash(hashHex, publicKey) {
  713. var smDigest = new SM3Digest();
  714. var z = new SM3Digest().getZ(G, publicKey.substr(2, 128));
  715. var zValue = _.hexToArray(_.arrayToHex(z).toString());
  716. var p = hashHex;
  717. var pValue = _.hexToArray(p);
  718. var hashData = new Array(smDigest.getDigestSize());
  719. smDigest.blockUpdate(zValue, 0, zValue.length);
  720. smDigest.blockUpdate(pValue, 0, pValue.length);
  721. smDigest.doFinal(hashData, 0);
  722. return _.arrayToHex(hashData).toString();
  723. }
  724. /**
  725. * 计算公钥
  726. */
  727. function getPublicKeyFromPrivateKey(privateKey) {
  728. var PA = G.multiply(new BigInteger(privateKey, 16));
  729. var x = _.leftPad(PA.getX().toBigInteger().toString(16), 64);
  730. var y = _.leftPad(PA.getY().toBigInteger().toString(16), 64);
  731. return '04' + x + y;
  732. }
  733. /**
  734. * 获取椭圆曲线点
  735. */
  736. function getPoint() {
  737. var keypair = _.generateKeyPairHex();
  738. var PA = curve.decodePointHex(keypair.publicKey);
  739. keypair.k = new BigInteger(keypair.privateKey, 16);
  740. keypair.x1 = PA.getX().toBigInteger();
  741. return keypair;
  742. }
  743. module.exports = {
  744. generateKeyPairHex: _.generateKeyPairHex,
  745. doEncrypt: doEncrypt,
  746. doDecrypt: doDecrypt,
  747. doSignature: doSignature,
  748. doVerifySignature: doVerifySignature,
  749. getPoint: getPoint
  750. };
  751. /***/ }),
  752. /* 5 */
  753. /***/ (function(module, exports, __webpack_require__) {
  754. "use strict";
  755. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  756. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  757. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  758. /* eslint-disable class-methods-use-this */
  759. var _require = __webpack_require__(0),
  760. BigInteger = _require.BigInteger;
  761. function bigIntToMinTwosComplementsHex(bigIntegerValue) {
  762. var h = bigIntegerValue.toString(16);
  763. if (h.substr(0, 1) !== '-') {
  764. if (h.length % 2 === 1) {
  765. h = '0' + h;
  766. } else if (!h.match(/^[0-7]/)) {
  767. h = '00' + h;
  768. }
  769. } else {
  770. var hPos = h.substr(1);
  771. var xorLen = hPos.length;
  772. if (xorLen % 2 === 1) {
  773. xorLen += 1;
  774. } else if (!h.match(/^[0-7]/)) {
  775. xorLen += 2;
  776. }
  777. var hMask = '';
  778. for (var i = 0; i < xorLen; i++) {
  779. hMask += 'f';
  780. }
  781. var biMask = new BigInteger(hMask, 16);
  782. var biNeg = biMask.xor(bigIntegerValue).add(BigInteger.ONE);
  783. h = biNeg.toString(16).replace(/^-/, '');
  784. }
  785. return h;
  786. }
  787. /**
  788. * base class for ASN.1 DER encoder object
  789. */
  790. var ASN1Object = function () {
  791. function ASN1Object() {
  792. _classCallCheck(this, ASN1Object);
  793. this.isModified = true;
  794. this.hTLV = null;
  795. this.hT = '00';
  796. this.hL = '00';
  797. this.hV = '';
  798. }
  799. /**
  800. * get hexadecimal ASN.1 TLV length(L) bytes from TLV value(V)
  801. */
  802. ASN1Object.prototype.getLengthHexFromValue = function getLengthHexFromValue() {
  803. var n = this.hV.length / 2;
  804. var hN = n.toString(16);
  805. if (hN.length % 2 === 1) {
  806. hN = '0' + hN;
  807. }
  808. if (n < 128) {
  809. return hN;
  810. } else {
  811. var hNlen = hN.length / 2;
  812. var head = 128 + hNlen;
  813. return head.toString(16) + hN;
  814. }
  815. };
  816. /**
  817. * get hexadecimal string of ASN.1 TLV bytes
  818. */
  819. ASN1Object.prototype.getEncodedHex = function getEncodedHex() {
  820. if (this.hTLV == null || this.isModified) {
  821. this.hV = this.getFreshValueHex();
  822. this.hL = this.getLengthHexFromValue();
  823. this.hTLV = this.hT + this.hL + this.hV;
  824. this.isModified = false;
  825. }
  826. return this.hTLV;
  827. };
  828. ASN1Object.prototype.getFreshValueHex = function getFreshValueHex() {
  829. return '';
  830. };
  831. return ASN1Object;
  832. }();
  833. /**
  834. * class for ASN.1 DER Integer
  835. */
  836. var DERInteger = function (_ASN1Object) {
  837. _inherits(DERInteger, _ASN1Object);
  838. function DERInteger(options) {
  839. _classCallCheck(this, DERInteger);
  840. var _this = _possibleConstructorReturn(this, _ASN1Object.call(this));
  841. _this.hT = '02';
  842. if (options && options.bigint) {
  843. _this.hTLV = null;
  844. _this.isModified = true;
  845. _this.hV = bigIntToMinTwosComplementsHex(options.bigint);
  846. }
  847. return _this;
  848. }
  849. DERInteger.prototype.getFreshValueHex = function getFreshValueHex() {
  850. return this.hV;
  851. };
  852. return DERInteger;
  853. }(ASN1Object);
  854. /**
  855. * class for ASN.1 DER Sequence
  856. */
  857. var DERSequence = function (_ASN1Object2) {
  858. _inherits(DERSequence, _ASN1Object2);
  859. function DERSequence(options) {
  860. _classCallCheck(this, DERSequence);
  861. var _this2 = _possibleConstructorReturn(this, _ASN1Object2.call(this));
  862. _this2.hT = '30';
  863. _this2.asn1Array = [];
  864. if (options && options.array) {
  865. _this2.asn1Array = options.array;
  866. }
  867. return _this2;
  868. }
  869. DERSequence.prototype.getFreshValueHex = function getFreshValueHex() {
  870. var h = '';
  871. for (var i = 0; i < this.asn1Array.length; i++) {
  872. var asn1Obj = this.asn1Array[i];
  873. h += asn1Obj.getEncodedHex();
  874. }
  875. this.hV = h;
  876. return this.hV;
  877. };
  878. return DERSequence;
  879. }(ASN1Object);
  880. /**
  881. * get byte length for ASN.1 L(length) bytes
  882. */
  883. function getByteLengthOfL(s, pos) {
  884. if (s.substring(pos + 2, pos + 3) !== '8') return 1;
  885. var i = parseInt(s.substring(pos + 3, pos + 4), 10);
  886. if (i === 0) return -1; // length octet '80' indefinite length
  887. if (i > 0 && i < 10) return i + 1; // including '8?' octet;
  888. return -2; // malformed format
  889. }
  890. /**
  891. * get hexadecimal string for ASN.1 L(length) bytes
  892. */
  893. function getHexOfL(s, pos) {
  894. var len = getByteLengthOfL(s, pos);
  895. if (len < 1) return '';
  896. return s.substring(pos + 2, pos + 2 + len * 2);
  897. }
  898. /**
  899. * get integer value of ASN.1 length for ASN.1 data
  900. */
  901. function getIntOfL(s, pos) {
  902. var hLength = getHexOfL(s, pos);
  903. if (hLength === '') return -1;
  904. var bi = void 0;
  905. if (parseInt(hLength.substring(0, 1), 10) < 8) {
  906. bi = new BigInteger(hLength, 16);
  907. } else {
  908. bi = new BigInteger(hLength.substring(2), 16);
  909. }
  910. return bi.intValue();
  911. }
  912. /**
  913. * get ASN.1 value starting string position for ASN.1 object refered by index 'idx'.
  914. */
  915. function getStartPosOfV(s, pos) {
  916. var lLen = getByteLengthOfL(s, pos);
  917. if (lLen < 0) return lLen;
  918. return pos + (lLen + 1) * 2;
  919. }
  920. /**
  921. * get hexadecimal string of ASN.1 V(value)
  922. */
  923. function getHexOfV(s, pos) {
  924. var pos1 = getStartPosOfV(s, pos);
  925. var len = getIntOfL(s, pos);
  926. return s.substring(pos1, pos1 + len * 2);
  927. }
  928. /**
  929. * get next sibling starting index for ASN.1 object string
  930. */
  931. function getPosOfNextSibling(s, pos) {
  932. var pos1 = getStartPosOfV(s, pos);
  933. var len = getIntOfL(s, pos);
  934. return pos1 + len * 2;
  935. }
  936. /**
  937. * get array of indexes of child ASN.1 objects
  938. */
  939. function getPosArrayOfChildren(h, pos) {
  940. var a = [];
  941. var p0 = getStartPosOfV(h, pos);
  942. a.push(p0);
  943. var len = getIntOfL(h, pos);
  944. var p = p0;
  945. var k = 0;
  946. for (;;) {
  947. var pNext = getPosOfNextSibling(h, p);
  948. if (pNext == null || pNext - p0 >= len * 2) break;
  949. if (k >= 200) break;
  950. a.push(pNext);
  951. p = pNext;
  952. k++;
  953. }
  954. return a;
  955. }
  956. module.exports = {
  957. /**
  958. * ASN.1 DER编码
  959. */
  960. encodeDer: function encodeDer(r, s) {
  961. var derR = new DERInteger({ bigint: r });
  962. var derS = new DERInteger({ bigint: s });
  963. var derSeq = new DERSequence({ array: [derR, derS] });
  964. return derSeq.getEncodedHex();
  965. },
  966. /**
  967. * 解析 ASN.1 DER
  968. */
  969. decodeDer: function decodeDer(input) {
  970. // 1. Items of ASN.1 Sequence Check
  971. var a = getPosArrayOfChildren(input, 0);
  972. // 2. Integer check
  973. var iTLV1 = a[0];
  974. var iTLV2 = a[1];
  975. // 3. getting value
  976. var hR = getHexOfV(input, iTLV1);
  977. var hS = getHexOfV(input, iTLV2);
  978. var r = new BigInteger(hR, 16);
  979. var s = new BigInteger(hS, 16);
  980. return { r: r, s: s };
  981. }
  982. };
  983. /***/ }),
  984. /* 6 */
  985. /***/ (function(module, exports, __webpack_require__) {
  986. "use strict";
  987. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  988. /* eslint-disable no-case-declarations, max-len */
  989. var _require = __webpack_require__(0),
  990. BigInteger = _require.BigInteger;
  991. /**
  992. * thanks for Tom Wu : http://www-cs-students.stanford.edu/~tjw/jsbn/
  993. *
  994. * Basic Javascript Elliptic Curve implementation
  995. * Ported loosely from BouncyCastle's Java EC code
  996. * Only Fp curves implemented for now
  997. */
  998. var THREE = new BigInteger('3');
  999. /**
  1000. * 椭圆曲线域元素
  1001. */
  1002. var ECFieldElementFp = function () {
  1003. function ECFieldElementFp(q, x) {
  1004. _classCallCheck(this, ECFieldElementFp);
  1005. this.x = x;
  1006. this.q = q;
  1007. // TODO if (x.compareTo(q) >= 0) error
  1008. }
  1009. /**
  1010. * 判断相等
  1011. */
  1012. ECFieldElementFp.prototype.equals = function equals(other) {
  1013. if (other === this) return true;
  1014. return this.q.equals(other.q) && this.x.equals(other.x);
  1015. };
  1016. /**
  1017. * 返回具体数值
  1018. */
  1019. ECFieldElementFp.prototype.toBigInteger = function toBigInteger() {
  1020. return this.x;
  1021. };
  1022. /**
  1023. * 取反
  1024. */
  1025. ECFieldElementFp.prototype.negate = function negate() {
  1026. return new ECFieldElementFp(this.q, this.x.negate().mod(this.q));
  1027. };
  1028. /**
  1029. * 相加
  1030. */
  1031. ECFieldElementFp.prototype.add = function add(b) {
  1032. return new ECFieldElementFp(this.q, this.x.add(b.toBigInteger()).mod(this.q));
  1033. };
  1034. /**
  1035. * 相减
  1036. */
  1037. ECFieldElementFp.prototype.subtract = function subtract(b) {
  1038. return new ECFieldElementFp(this.q, this.x.subtract(b.toBigInteger()).mod(this.q));
  1039. };
  1040. /**
  1041. * 相乘
  1042. */
  1043. ECFieldElementFp.prototype.multiply = function multiply(b) {
  1044. return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger()).mod(this.q));
  1045. };
  1046. /**
  1047. * 相除
  1048. */
  1049. ECFieldElementFp.prototype.divide = function divide(b) {
  1050. return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger().modInverse(this.q)).mod(this.q));
  1051. };
  1052. /**
  1053. * 平方
  1054. */
  1055. ECFieldElementFp.prototype.square = function square() {
  1056. return new ECFieldElementFp(this.q, this.x.square().mod(this.q));
  1057. };
  1058. return ECFieldElementFp;
  1059. }();
  1060. var ECPointFp = function () {
  1061. function ECPointFp(curve, x, y, z) {
  1062. _classCallCheck(this, ECPointFp);
  1063. this.curve = curve;
  1064. this.x = x;
  1065. this.y = y;
  1066. // 标准射影坐标系:zinv == null 或 z * zinv == 1
  1067. this.z = z == null ? BigInteger.ONE : z;
  1068. this.zinv = null;
  1069. // TODO: compression flag
  1070. }
  1071. ECPointFp.prototype.getX = function getX() {
  1072. if (this.zinv === null) this.zinv = this.z.modInverse(this.curve.q);
  1073. return this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q));
  1074. };
  1075. ECPointFp.prototype.getY = function getY() {
  1076. if (this.zinv === null) this.zinv = this.z.modInverse(this.curve.q);
  1077. return this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q));
  1078. };
  1079. /**
  1080. * 判断相等
  1081. */
  1082. ECPointFp.prototype.equals = function equals(other) {
  1083. if (other === this) return true;
  1084. if (this.isInfinity()) return other.isInfinity();
  1085. if (other.isInfinity()) return this.isInfinity();
  1086. // u = y2 * z1 - y1 * z2
  1087. var u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q);
  1088. if (!u.equals(BigInteger.ZERO)) return false;
  1089. // v = x2 * z1 - x1 * z2
  1090. var v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q);
  1091. return v.equals(BigInteger.ZERO);
  1092. };
  1093. /**
  1094. * 是否是无穷远点
  1095. */
  1096. ECPointFp.prototype.isInfinity = function isInfinity() {
  1097. if (this.x === null && this.y === null) return true;
  1098. return this.z.equals(BigInteger.ZERO) && !this.y.toBigInteger().equals(BigInteger.ZERO);
  1099. };
  1100. /**
  1101. * 取反,x 轴对称点
  1102. */
  1103. ECPointFp.prototype.negate = function negate() {
  1104. return new ECPointFp(this.curve, this.x, this.y.negate(), this.z);
  1105. };
  1106. /**
  1107. * 相加
  1108. *
  1109. * 标准射影坐标系:
  1110. *
  1111. * λ1 = x1 * z2
  1112. * λ2 = x2 * z1
  1113. * λ3 = λ1 − λ2
  1114. * λ4 = y1 * z2
  1115. * λ5 = y2 * z1
  1116. * λ6 = λ4 − λ5
  1117. * λ7 = λ1 + λ2
  1118. * λ8 = z1 * z2
  1119. * λ9 = λ3^2
  1120. * λ10 = λ3 * λ9
  1121. * λ11 = λ8 * λ6^2 − λ7 * λ9
  1122. * x3 = λ3 * λ11
  1123. * y3 = λ6 * (λ9 * λ1 − λ11) − λ4 * λ10
  1124. * z3 = λ10 * λ8
  1125. */
  1126. ECPointFp.prototype.add = function add(b) {
  1127. if (this.isInfinity()) return b;
  1128. if (b.isInfinity()) return this;
  1129. var x1 = this.x.toBigInteger();
  1130. var y1 = this.y.toBigInteger();
  1131. var z1 = this.z;
  1132. var x2 = b.x.toBigInteger();
  1133. var y2 = b.y.toBigInteger();
  1134. var z2 = b.z;
  1135. var q = this.curve.q;
  1136. var w1 = x1.multiply(z2).mod(q);
  1137. var w2 = x2.multiply(z1).mod(q);
  1138. var w3 = w1.subtract(w2);
  1139. var w4 = y1.multiply(z2).mod(q);
  1140. var w5 = y2.multiply(z1).mod(q);
  1141. var w6 = w4.subtract(w5);
  1142. if (BigInteger.ZERO.equals(w3)) {
  1143. if (BigInteger.ZERO.equals(w6)) {
  1144. return this.twice(); // this == b,计算自加
  1145. }
  1146. return this.curve.infinity; // this == -b,则返回无穷远点
  1147. }
  1148. var w7 = w1.add(w2);
  1149. var w8 = z1.multiply(z2).mod(q);
  1150. var w9 = w3.square().mod(q);
  1151. var w10 = w3.multiply(w9).mod(q);
  1152. var w11 = w8.multiply(w6.square()).subtract(w7.multiply(w9)).mod(q);
  1153. var x3 = w3.multiply(w11).mod(q);
  1154. var y3 = w6.multiply(w9.multiply(w1).subtract(w11)).subtract(w4.multiply(w10)).mod(q);
  1155. var z3 = w10.multiply(w8).mod(q);
  1156. return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
  1157. };
  1158. /**
  1159. * 自加
  1160. *
  1161. * 标准射影坐标系:
  1162. *
  1163. * λ1 = 3 * x1^2 + a * z1^2
  1164. * λ2 = 2 * y1 * z1
  1165. * λ3 = y1^2
  1166. * λ4 = λ3 * x1 * z1
  1167. * λ5 = λ2^2
  1168. * λ6 = λ1^2 − 8 * λ4
  1169. * x3 = λ2 * λ6
  1170. * y3 = λ1 * (4 * λ4 − λ6) − 2 * λ5 * λ3
  1171. * z3 = λ2 * λ5
  1172. */
  1173. ECPointFp.prototype.twice = function twice() {
  1174. if (this.isInfinity()) return this;
  1175. if (!this.y.toBigInteger().signum()) return this.curve.infinity;
  1176. var x1 = this.x.toBigInteger();
  1177. var y1 = this.y.toBigInteger();
  1178. var z1 = this.z;
  1179. var q = this.curve.q;
  1180. var a = this.curve.a.toBigInteger();
  1181. var w1 = x1.square().multiply(THREE).add(a.multiply(z1.square())).mod(q);
  1182. var w2 = y1.shiftLeft(1).multiply(z1).mod(q);
  1183. var w3 = y1.square().mod(q);
  1184. var w4 = w3.multiply(x1).multiply(z1).mod(q);
  1185. var w5 = w2.square().mod(q);
  1186. var w6 = w1.square().subtract(w4.shiftLeft(3)).mod(q);
  1187. var x3 = w2.multiply(w6).mod(q);
  1188. var y3 = w1.multiply(w4.shiftLeft(2).subtract(w6)).subtract(w5.shiftLeft(1).multiply(w3)).mod(q);
  1189. var z3 = w2.multiply(w5).mod(q);
  1190. return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
  1191. };
  1192. /**
  1193. * 倍点计算
  1194. */
  1195. ECPointFp.prototype.multiply = function multiply(k) {
  1196. if (this.isInfinity()) return this;
  1197. if (!k.signum()) return this.curve.infinity;
  1198. // 使用加减法
  1199. var k3 = k.multiply(THREE);
  1200. var neg = this.negate();
  1201. var Q = this;
  1202. for (var i = k3.bitLength() - 2; i > 0; i--) {
  1203. Q = Q.twice();
  1204. var k3Bit = k3.testBit(i);
  1205. var kBit = k.testBit(i);
  1206. if (k3Bit !== kBit) {
  1207. Q = Q.add(k3Bit ? this : neg);
  1208. }
  1209. }
  1210. return Q;
  1211. };
  1212. return ECPointFp;
  1213. }();
  1214. /**
  1215. * 椭圆曲线 y^2 = x^3 + ax + b
  1216. */
  1217. var ECCurveFp = function () {
  1218. function ECCurveFp(q, a, b) {
  1219. _classCallCheck(this, ECCurveFp);
  1220. this.q = q;
  1221. this.a = this.fromBigInteger(a);
  1222. this.b = this.fromBigInteger(b);
  1223. this.infinity = new ECPointFp(this, null, null); // 无穷远点
  1224. }
  1225. /**
  1226. * 判断两个椭圆曲线是否相等
  1227. */
  1228. ECCurveFp.prototype.equals = function equals(other) {
  1229. if (other === this) return true;
  1230. return this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b);
  1231. };
  1232. /**
  1233. * 生成椭圆曲线域元素
  1234. */
  1235. ECCurveFp.prototype.fromBigInteger = function fromBigInteger(x) {
  1236. return new ECFieldElementFp(this.q, x);
  1237. };
  1238. /**
  1239. * 解析 16 进制串为椭圆曲线点
  1240. */
  1241. ECCurveFp.prototype.decodePointHex = function decodePointHex(s) {
  1242. switch (parseInt(s.substr(0, 2), 16)) {
  1243. // 第一个字节
  1244. case 0:
  1245. return this.infinity;
  1246. case 2:
  1247. case 3:
  1248. // 不支持的压缩方式
  1249. return null;
  1250. case 4:
  1251. case 6:
  1252. case 7:
  1253. var len = (s.length - 2) / 2;
  1254. var xHex = s.substr(2, len);
  1255. var yHex = s.substr(len + 2, len);
  1256. return new ECPointFp(this, this.fromBigInteger(new BigInteger(xHex, 16)), this.fromBigInteger(new BigInteger(yHex, 16)));
  1257. default:
  1258. // 不支持
  1259. return null;
  1260. }
  1261. };
  1262. return ECCurveFp;
  1263. }();
  1264. module.exports = {
  1265. ECPointFp: ECPointFp,
  1266. ECCurveFp: ECCurveFp
  1267. };
  1268. /***/ }),
  1269. /* 7 */
  1270. /***/ (function(module, exports, __webpack_require__) {
  1271. "use strict";
  1272. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  1273. /* eslint-disable no-bitwise, no-mixed-operators, class-methods-use-this */
  1274. var _require = __webpack_require__(0),
  1275. BigInteger = _require.BigInteger;
  1276. var SM3Digest = __webpack_require__(2);
  1277. var _ = __webpack_require__(1);
  1278. var SM2Cipher = function () {
  1279. function SM2Cipher() {
  1280. _classCallCheck(this, SM2Cipher);
  1281. this.ct = 1;
  1282. this.p2 = null;
  1283. this.sm3keybase = null;
  1284. this.sm3c3 = null;
  1285. this.key = new Array(32);
  1286. this.keyOff = 0;
  1287. }
  1288. SM2Cipher.prototype.reset = function reset() {
  1289. this.sm3keybase = new SM3Digest();
  1290. this.sm3c3 = new SM3Digest();
  1291. var xWords = _.hexToArray(this.p2.getX().toBigInteger().toRadix(16));
  1292. var yWords = _.hexToArray(this.p2.getY().toBigInteger().toRadix(16));
  1293. this.sm3keybase.blockUpdate(xWords, 0, xWords.length);
  1294. this.sm3c3.blockUpdate(xWords, 0, xWords.length);
  1295. this.sm3keybase.blockUpdate(yWords, 0, yWords.length);
  1296. this.ct = 1;
  1297. this.nextKey();
  1298. };
  1299. SM2Cipher.prototype.nextKey = function nextKey() {
  1300. var sm3keycur = new SM3Digest(this.sm3keybase);
  1301. sm3keycur.update(this.ct >> 24 & 0x00ff);
  1302. sm3keycur.update(this.ct >> 16 & 0x00ff);
  1303. sm3keycur.update(this.ct >> 8 & 0x00ff);
  1304. sm3keycur.update(this.ct & 0x00ff);
  1305. sm3keycur.doFinal(this.key, 0);
  1306. this.keyOff = 0;
  1307. this.ct++;
  1308. };
  1309. SM2Cipher.prototype.initEncipher = function initEncipher(userKey) {
  1310. var keypair = _.generateKeyPairHex();
  1311. var k = new BigInteger(keypair.privateKey, 16);
  1312. var publicKey = keypair.publicKey;
  1313. this.p2 = userKey.multiply(k); // [k](Pb)
  1314. this.reset();
  1315. if (publicKey.length > 128) {
  1316. publicKey = publicKey.substr(publicKey.length - 128);
  1317. }
  1318. return publicKey;
  1319. };
  1320. SM2Cipher.prototype.encryptBlock = function encryptBlock(data) {
  1321. this.sm3c3.blockUpdate(data, 0, data.length);
  1322. for (var i = 0; i < data.length; i++) {
  1323. if (this.keyOff === this.key.length) {
  1324. this.nextKey();
  1325. }
  1326. data[i] ^= this.key[this.keyOff++] & 0xff;
  1327. }
  1328. };
  1329. SM2Cipher.prototype.initDecipher = function initDecipher(userD, c1) {
  1330. this.p2 = c1.multiply(userD);
  1331. this.reset();
  1332. };
  1333. SM2Cipher.prototype.decryptBlock = function decryptBlock(data) {
  1334. for (var i = 0; i < data.length; i++) {
  1335. if (this.keyOff === this.key.length) {
  1336. this.nextKey();
  1337. }
  1338. data[i] ^= this.key[this.keyOff++] & 0xff;
  1339. }
  1340. this.sm3c3.blockUpdate(data, 0, data.length);
  1341. };
  1342. SM2Cipher.prototype.doFinal = function doFinal(c3) {
  1343. var yWords = _.hexToArray(this.p2.getY().toBigInteger().toRadix(16));
  1344. this.sm3c3.blockUpdate(yWords, 0, yWords.length);
  1345. this.sm3c3.doFinal(c3, 0);
  1346. this.reset();
  1347. };
  1348. SM2Cipher.prototype.createPoint = function createPoint(x, y) {
  1349. var publicKey = '04' + x + y;
  1350. var point = _.getGlobalCurve().decodePointHex(publicKey);
  1351. return point;
  1352. };
  1353. return SM2Cipher;
  1354. }();
  1355. module.exports = SM2Cipher;
  1356. /***/ }),
  1357. /* 8 */
  1358. /***/ (function(module, exports, __webpack_require__) {
  1359. "use strict";
  1360. /**
  1361. * 左补0到指定长度
  1362. */
  1363. function leftPad(input, num) {
  1364. if (input.length >= num) return input;
  1365. return new Array(num - input.length + 1).join('0') + input;
  1366. }
  1367. /**
  1368. * 二进制转化为十六进制
  1369. */
  1370. function binary2hex(binary) {
  1371. var binaryLength = 8;
  1372. var hex = '';
  1373. for (var i = 0; i < binary.length / binaryLength; i++) {
  1374. hex += leftPad(parseInt(binary.substr(i * binaryLength, binaryLength), 2).toString(16), 2);
  1375. }
  1376. return hex;
  1377. }
  1378. /**
  1379. * 十六进制转化为二进制
  1380. */
  1381. function hex2binary(hex) {
  1382. var hexLength = 2;
  1383. var binary = '';
  1384. for (var i = 0; i < hex.length / hexLength; i++) {
  1385. binary += leftPad(parseInt(hex.substr(i * hexLength, hexLength), 16).toString(2), 8);
  1386. }
  1387. return binary;
  1388. }
  1389. /**
  1390. * 普通字符串转化为二进制
  1391. */
  1392. function str2binary(str) {
  1393. var binary = '';
  1394. for (var i = 0, len = str.length; i < len; i++) {
  1395. var ch = str[i];
  1396. binary += leftPad(ch.codePointAt(0).toString(2), 8);
  1397. }
  1398. return binary;
  1399. }
  1400. /**
  1401. * 循环左移
  1402. */
  1403. function rol(str, n) {
  1404. return str.substring(n % str.length) + str.substr(0, n % str.length);
  1405. }
  1406. /**
  1407. * 二进制运算
  1408. */
  1409. function binaryCal(x, y, method) {
  1410. var a = x || '';
  1411. var b = y || '';
  1412. var result = [];
  1413. var prevResult = void 0;
  1414. for (var i = a.length - 1; i >= 0; i--) {
  1415. // 大端
  1416. prevResult = method(a[i], b[i], prevResult);
  1417. result[i] = prevResult[0];
  1418. }
  1419. return result.join('');
  1420. }
  1421. /**
  1422. * 二进制异或运算
  1423. */
  1424. function xor(x, y) {
  1425. return binaryCal(x, y, function (a, b) {
  1426. return [a === b ? '0' : '1'];
  1427. });
  1428. }
  1429. /**
  1430. * 二进制与运算
  1431. */
  1432. function and(x, y) {
  1433. return binaryCal(x, y, function (a, b) {
  1434. return [a === '1' && b === '1' ? '1' : '0'];
  1435. });
  1436. }
  1437. /**
  1438. * 二进制或运算
  1439. */
  1440. function or(x, y) {
  1441. return binaryCal(x, y, function (a, b) {
  1442. return [a === '1' || b === '1' ? '1' : '0'];
  1443. }); // a === '0' && b === '0' ? '0' : '1'
  1444. }
  1445. /**
  1446. * 二进制与运算
  1447. */
  1448. function add(x, y) {
  1449. var result = binaryCal(x, y, function (a, b, prevResult) {
  1450. var carry = prevResult ? prevResult[1] : '0' || '0';
  1451. // a,b不等时,carry不变,结果与carry相反
  1452. // a,b相等时,结果等于原carry,新carry等于a
  1453. if (a !== b) return [carry === '0' ? '1' : '0', carry];
  1454. return [carry, a];
  1455. });
  1456. return result;
  1457. }
  1458. /**
  1459. * 二进制非运算
  1460. */
  1461. function not(x) {
  1462. return binaryCal(x, undefined, function (a) {
  1463. return [a === '1' ? '0' : '1'];
  1464. });
  1465. }
  1466. function calMulti(method) {
  1467. return function () {
  1468. for (var _len = arguments.length, arr = Array(_len), _key = 0; _key < _len; _key++) {
  1469. arr[_key] = arguments[_key];
  1470. }
  1471. return arr.reduce(function (prev, curr) {
  1472. return method(prev, curr);
  1473. });
  1474. };
  1475. }
  1476. /**
  1477. * 压缩函数中的置换函数 P1(X) = X xor (X <<< 9) xor (X <<< 17)
  1478. */
  1479. function P0(X) {
  1480. return calMulti(xor)(X, rol(X, 9), rol(X, 17));
  1481. }
  1482. /**
  1483. * 消息扩展中的置换函数 P1(X) = X xor (X <<< 15) xor (X <<< 23)
  1484. */
  1485. function P1(X) {
  1486. return calMulti(xor)(X, rol(X, 15), rol(X, 23));
  1487. }
  1488. function FF(X, Y, Z, j) {
  1489. return j >= 0 && j <= 15 ? calMulti(xor)(X, Y, Z) : calMulti(or)(and(X, Y), and(X, Z), and(Y, Z));
  1490. }
  1491. function GG(X, Y, Z, j) {
  1492. return j >= 0 && j <= 15 ? calMulti(xor)(X, Y, Z) : or(and(X, Y), and(not(X), Z));
  1493. }
  1494. function T(j) {
  1495. return j >= 0 && j <= 15 ? hex2binary('79cc4519') : hex2binary('7a879d8a');
  1496. }
  1497. /**
  1498. * 压缩函数
  1499. */
  1500. function CF(V, Bi) {
  1501. // 消息扩展
  1502. var wordLength = 32;
  1503. var W = [];
  1504. var M = []; // W'
  1505. // 将消息分组B划分为16个字W0, W1,…… ,W15 (字为长度为32的比特串)
  1506. for (var i = 0; i < 16; i++) {
  1507. W.push(Bi.substr(i * wordLength, wordLength));
  1508. }
  1509. // W[j] <- P1(W[j−16] xor W[j−9] xor (W[j−3] <<< 15)) xor (W[j−13] <<< 7) xor W[j−6]
  1510. for (var j = 16; j < 68; j++) {
  1511. W.push(calMulti(xor)(P1(calMulti(xor)(W[j - 16], W[j - 9], rol(W[j - 3], 15))), rol(W[j - 13], 7), W[j - 6]));
  1512. }
  1513. // W′[j] = W[j] xor W[j+4]
  1514. for (var _j = 0; _j < 64; _j++) {
  1515. M.push(xor(W[_j], W[_j + 4]));
  1516. }
  1517. // 压缩
  1518. var wordRegister = []; // 字寄存器
  1519. for (var _j2 = 0; _j2 < 8; _j2++) {
  1520. wordRegister.push(V.substr(_j2 * wordLength, wordLength));
  1521. }
  1522. var A = wordRegister[0];
  1523. var B = wordRegister[1];
  1524. var C = wordRegister[2];
  1525. var D = wordRegister[3];
  1526. var E = wordRegister[4];
  1527. var F = wordRegister[5];
  1528. var G = wordRegister[6];
  1529. var H = wordRegister[7];
  1530. // 中间变量
  1531. var SS1 = void 0;
  1532. var SS2 = void 0;
  1533. var TT1 = void 0;
  1534. var TT2 = void 0;
  1535. for (var _j3 = 0; _j3 < 64; _j3++) {
  1536. SS1 = rol(calMulti(add)(rol(A, 12), E, rol(T(_j3), _j3)), 7);
  1537. SS2 = xor(SS1, rol(A, 12));
  1538. TT1 = calMulti(add)(FF(A, B, C, _j3), D, SS2, M[_j3]);
  1539. TT2 = calMulti(add)(GG(E, F, G, _j3), H, SS1, W[_j3]);
  1540. D = C;
  1541. C = rol(B, 9);
  1542. B = A;
  1543. A = TT1;
  1544. H = G;
  1545. G = rol(F, 19);
  1546. F = E;
  1547. E = P0(TT2);
  1548. }
  1549. return xor([A, B, C, D, E, F, G, H].join(''), V);
  1550. }
  1551. module.exports = function (str) {
  1552. var binary = str2binary(str);
  1553. // 填充
  1554. var len = binary.length;
  1555. // k是满足len + 1 + k = 448mod512的最小的非负整数
  1556. var k = len % 512;
  1557. // 如果 448 <= (512 % len) < 512,需要多补充 (len % 448) 比特'0'以满足总比特长度为512的倍数
  1558. k = k >= 448 ? 512 - k % 448 - 1 : 448 - k - 1;
  1559. var m = (binary + '1' + leftPad('', k) + leftPad(len.toString(2), 64)).toString(); // k个0
  1560. // 迭代压缩
  1561. var n = (len + k + 65) / 512;
  1562. var V = hex2binary('7380166f4914b2b9172442d7da8a0600a96f30bc163138aae38dee4db0fb0e4e');
  1563. for (var i = 0; i <= n - 1; i++) {
  1564. var B = m.substr(512 * i, 512);
  1565. V = CF(V, B);
  1566. }
  1567. return binary2hex(V);
  1568. };
  1569. /***/ }),
  1570. /* 9 */
  1571. /***/ (function(module, exports, __webpack_require__) {
  1572. "use strict";
  1573. /* eslint-disable no-bitwise, no-mixed-operators */
  1574. var DECRYPT = 0;
  1575. var ROUND = 32;
  1576. var BLOCK = 16;
  1577. var Sbox = [0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05, 0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62, 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6, 0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8, 0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35, 0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87, 0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e, 0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1, 0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3, 0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f, 0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51, 0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8, 0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0, 0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84, 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48];
  1578. var CK = [0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9, 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279];
  1579. function rotl(x, y) {
  1580. return x << y | x >>> 32 - y;
  1581. }
  1582. function byteSub(a) {
  1583. return (Sbox[a >>> 24 & 0xFF] & 0xFF) << 24 | (Sbox[a >>> 16 & 0xFF] & 0xFF) << 16 | (Sbox[a >>> 8 & 0xFF] & 0xFF) << 8 | Sbox[a & 0xFF] & 0xFF;
  1584. }
  1585. function l1(b) {
  1586. return b ^ rotl(b, 2) ^ rotl(b, 10) ^ rotl(b, 18) ^ rotl(b, 24);
  1587. }
  1588. function l2(b) {
  1589. return b ^ rotl(b, 13) ^ rotl(b, 23);
  1590. }
  1591. function sms4Crypt(input, output, roundKey) {
  1592. var r = void 0;
  1593. var mid = void 0;
  1594. var x = new Array(4);
  1595. var tmp = new Array(4);
  1596. for (var i = 0; i < 4; i++) {
  1597. tmp[0] = input[0 + 4 * i] & 0xff;
  1598. tmp[1] = input[1 + 4 * i] & 0xff;
  1599. tmp[2] = input[2 + 4 * i] & 0xff;
  1600. tmp[3] = input[3 + 4 * i] & 0xff;
  1601. x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
  1602. }
  1603. for (r = 0; r < 32; r += 4) {
  1604. mid = x[1] ^ x[2] ^ x[3] ^ roundKey[r + 0];
  1605. mid = byteSub(mid);
  1606. x[0] ^= l1(mid); // x4
  1607. mid = x[2] ^ x[3] ^ x[0] ^ roundKey[r + 1];
  1608. mid = byteSub(mid);
  1609. x[1] ^= l1(mid); // x5
  1610. mid = x[3] ^ x[0] ^ x[1] ^ roundKey[r + 2];
  1611. mid = byteSub(mid);
  1612. x[2] ^= l1(mid); // x6
  1613. mid = x[0] ^ x[1] ^ x[2] ^ roundKey[r + 3];
  1614. mid = byteSub(mid);
  1615. x[3] ^= l1(mid); // x7
  1616. }
  1617. // Reverse
  1618. for (var j = 0; j < 16; j += 4) {
  1619. output[j] = x[3 - j / 4] >>> 24 & 0xff;
  1620. output[j + 1] = x[3 - j / 4] >>> 16 & 0xff;
  1621. output[j + 2] = x[3 - j / 4] >>> 8 & 0xff;
  1622. output[j + 3] = x[3 - j / 4] & 0xff;
  1623. }
  1624. }
  1625. function sms4KeyExt(key, roundKey, cryptFlag) {
  1626. var r = void 0;
  1627. var mid = void 0;
  1628. var x = new Array(4);
  1629. var tmp = new Array(4);
  1630. for (var i = 0; i < 4; i++) {
  1631. tmp[0] = key[0 + 4 * i] & 0xff;
  1632. tmp[1] = key[1 + 4 * i] & 0xff;
  1633. tmp[2] = key[2 + 4 * i] & 0xff;
  1634. tmp[3] = key[3 + 4 * i] & 0xff;
  1635. x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
  1636. }
  1637. x[0] ^= 0xa3b1bac6;
  1638. x[1] ^= 0x56aa3350;
  1639. x[2] ^= 0x677d9197;
  1640. x[3] ^= 0xb27022dc;
  1641. for (r = 0; r < 32; r += 4) {
  1642. mid = x[1] ^ x[2] ^ x[3] ^ CK[r + 0];
  1643. mid = byteSub(mid);
  1644. roundKey[r + 0] = x[0] ^= l2(mid); // roundKey0 = K4
  1645. mid = x[2] ^ x[3] ^ x[0] ^ CK[r + 1];
  1646. mid = byteSub(mid);
  1647. roundKey[r + 1] = x[1] ^= l2(mid); // roundKey1 = K5
  1648. mid = x[3] ^ x[0] ^ x[1] ^ CK[r + 2];
  1649. mid = byteSub(mid);
  1650. roundKey[r + 2] = x[2] ^= l2(mid); // roundKey2 = K6
  1651. mid = x[0] ^ x[1] ^ x[2] ^ CK[r + 3];
  1652. mid = byteSub(mid);
  1653. roundKey[r + 3] = x[3] ^= l2(mid); // roundKey3 = K7
  1654. }
  1655. // 解密时轮密钥使用顺序:roundKey31, roundKey30, ..., roundKey0
  1656. if (cryptFlag === DECRYPT) {
  1657. for (r = 0; r < 16; r++) {
  1658. mid = roundKey[r];
  1659. roundKey[r] = roundKey[31 - r];
  1660. roundKey[31 - r] = mid;
  1661. }
  1662. }
  1663. }
  1664. function sm4(inArray, key, cryptFlag) {
  1665. var outArray = [];
  1666. var point = 0;
  1667. var roundKey = new Array(ROUND);
  1668. sms4KeyExt(key, roundKey, cryptFlag);
  1669. var input = new Array(16);
  1670. var output = new Array(16);
  1671. var inLen = inArray.length;
  1672. while (inLen >= BLOCK) {
  1673. input = inArray.slice(point, point + 16);
  1674. sms4Crypt(input, output, roundKey);
  1675. for (var i = 0; i < BLOCK; i++) {
  1676. outArray[point + i] = output[i];
  1677. }
  1678. inLen -= BLOCK;
  1679. point += BLOCK;
  1680. }
  1681. return outArray;
  1682. }
  1683. module.exports = {
  1684. encrypt: function encrypt(inArray, key) {
  1685. return sm4(inArray, key, 1);
  1686. },
  1687. decrypt: function decrypt(inArray, key) {
  1688. return sm4(inArray, key, 0);
  1689. }
  1690. };
  1691. /***/ })
  1692. /******/ ]);