UNPKG

madeline-ton

Version:

Pure JS client-side implementation of the Telegram TON blockchain protocol

152 lines (127 loc) 5.43 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _poly = require("../crypto-sync/poly"); var _cryptoWorker = _interopRequireDefault(require("./crypto-worker")); var _cryptoSync = _interopRequireDefault(require("./crypto-sync")); var _cryptoWebcrypto = _interopRequireDefault(require("./crypto-webcrypto")); var _tools = require("../tools"); var CryptoAsync = _poly.useWorkers ? _cryptoWorker["default"] : _cryptoSync["default"]; if (_poly.useWebCrypto) { if (_poly.useWebCryptoSha1) { CryptoAsync.prototype.sha1 = _cryptoWebcrypto["default"].prototype.sha1; } CryptoAsync.prototype.sha256 = _cryptoWebcrypto["default"].prototype.sha256; CryptoAsync.prototype.igeEncrypt = _cryptoWebcrypto["default"].prototype.igeEncrypt; CryptoAsync.prototype.getCtr = _cryptoWebcrypto["default"].prototype.getCtr; } CryptoAsync.prototype.aesCalculate = /*#__PURE__*/ function () { var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/ _regenerator["default"].mark(function _callee(msgKey, authKey) { var toServer, sha256_a, sha256_b, aesKey, aesIv, _args = arguments; return _regenerator["default"].wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: toServer = _args.length > 2 && _args[2] !== undefined ? _args[2] : true; toServer = toServer ? 0 : 8; _context.t0 = Uint8Array; _context.next = 5; return this.sha256((0, _tools.bufferConcat)(msgKey, authKey.subarray(toServer, toServer + 36))); case 5: _context.t1 = _context.sent; sha256_a = new _context.t0(_context.t1); _context.t2 = Uint8Array; _context.next = 10; return this.sha256((0, _tools.bufferConcat)(authKey.subarray(40 + toServer, 76 + toServer), msgKey)); case 10: _context.t3 = _context.sent; sha256_b = new _context.t2(_context.t3); aesKey = (0, _tools.bufferConcat)(sha256_a.subarray(0, 8), sha256_b.subarray(8, 24), sha256_a.subarray(24, 32)); aesIv = (0, _tools.bufferConcat)(sha256_b.subarray(0, 8), sha256_a.subarray(8, 24), sha256_b.subarray(24, 32)); return _context.abrupt("return", [aesKey, aesIv]); case 15: case "end": return _context.stop(); } } }, _callee, this); })); return function (_x, _x2) { return _ref.apply(this, arguments); }; }(); CryptoAsync.prototype.oldAesCalculate = /*#__PURE__*/ function () { var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/ _regenerator["default"].mark(function _callee2(msgKey, authKey) { var toServer, sha1_a, sha1_b, sha1_c, sha1_d, aesKey, aesIv, _args2 = arguments; return _regenerator["default"].wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: toServer = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : true; toServer = toServer ? 0 : 8; _context2.t0 = Uint8Array; _context2.next = 5; return this.sha1((0, _tools.bufferConcat)(msgKey, authKey.subarray(toServer, toServer + 32))); case 5: _context2.t1 = _context2.sent; sha1_a = new _context2.t0(_context2.t1); _context2.t2 = Uint8Array; _context2.next = 10; return this.sha1((0, _tools.bufferConcat)(authKey.subarray(32 + toServer, 48 + toServer), msgKey, authKey.subarray(48 + toServer, 64 + toServer))); case 10: _context2.t3 = _context2.sent; sha1_b = new _context2.t2(_context2.t3); _context2.t4 = Uint8Array; _context2.next = 15; return this.sha1((0, _tools.bufferConcat)(authKey.subarray(64 + toServer, 96 + toServer), msgKey)); case 15: _context2.t5 = _context2.sent; sha1_c = new _context2.t4(_context2.t5); _context2.t6 = Uint8Array; _context2.next = 20; return this.sha1((0, _tools.bufferConcat)(msgKey, authKey.subarray(96 + toServer, 128 + toServer))); case 20: _context2.t7 = _context2.sent; sha1_d = new _context2.t6(_context2.t7); aesKey = (0, _tools.bufferConcat)(sha1_a.subarray(0, 8), (0, _tools.bufferConcat)(sha1_b.subarray(8, 20), sha1_c.subarray(4, 16))); aesIv = (0, _tools.bufferConcat)(sha1_a.subarray(8, 20), (0, _tools.bufferConcat)(sha1_b.subarray(0, 8), sha1_c.subarray(16, 20))); aesIv = (0, _tools.bufferConcat)(aesIv, sha1_d.subarray(0, 8)); return _context2.abrupt("return", [aesKey, aesIv]); case 26: case "end": return _context2.stop(); } } }, _callee2, this); })); return function (_x3, _x4) { return _ref2.apply(this, arguments); }; }(); var _default = CryptoAsync; exports["default"] = _default;