aes-decrypter.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.aesDecrypter = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. /**
  3. * @file aes.js
  4. *
  5. * This file contains an adaptation of the AES decryption algorithm
  6. * from the Standford Javascript Cryptography Library. That work is
  7. * covered by the following copyright and permissions notice:
  8. *
  9. * Copyright 2009-2010 Emily Stark, Mike Hamburg, Dan Boneh.
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are
  14. * met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  25. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  33. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  34. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * The views and conclusions contained in the software and documentation
  37. * are those of the authors and should not be interpreted as representing
  38. * official policies, either expressed or implied, of the authors.
  39. */
  40. /**
  41. * Expand the S-box tables.
  42. *
  43. * @private
  44. */
  45. 'use strict';
  46. Object.defineProperty(exports, '__esModule', {
  47. value: true
  48. });
  49. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  50. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  51. var precompute = function precompute() {
  52. var tables = [[[], [], [], [], []], [[], [], [], [], []]];
  53. var encTable = tables[0];
  54. var decTable = tables[1];
  55. var sbox = encTable[4];
  56. var sboxInv = decTable[4];
  57. var i = undefined;
  58. var x = undefined;
  59. var xInv = undefined;
  60. var d = [];
  61. var th = [];
  62. var x2 = undefined;
  63. var x4 = undefined;
  64. var x8 = undefined;
  65. var s = undefined;
  66. var tEnc = undefined;
  67. var tDec = undefined;
  68. // Compute double and third tables
  69. for (i = 0; i < 256; i++) {
  70. th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
  71. }
  72. for (x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
  73. // Compute sbox
  74. s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
  75. s = s >> 8 ^ s & 255 ^ 99;
  76. sbox[x] = s;
  77. sboxInv[s] = x;
  78. // Compute MixColumns
  79. x8 = d[x4 = d[x2 = d[x]]];
  80. tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
  81. tEnc = d[s] * 0x101 ^ s * 0x1010100;
  82. for (i = 0; i < 4; i++) {
  83. encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
  84. decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
  85. }
  86. }
  87. // Compactify. Considerable speedup on Firefox.
  88. for (i = 0; i < 5; i++) {
  89. encTable[i] = encTable[i].slice(0);
  90. decTable[i] = decTable[i].slice(0);
  91. }
  92. return tables;
  93. };
  94. var aesTables = null;
  95. /**
  96. * Schedule out an AES key for both encryption and decryption. This
  97. * is a low-level class. Use a cipher mode to do bulk encryption.
  98. *
  99. * @class AES
  100. * @param key {Array} The key as an array of 4, 6 or 8 words.
  101. */
  102. var AES = (function () {
  103. function AES(key) {
  104. _classCallCheck(this, AES);
  105. /**
  106. * The expanded S-box and inverse S-box tables. These will be computed
  107. * on the client so that we don't have to send them down the wire.
  108. *
  109. * There are two tables, _tables[0] is for encryption and
  110. * _tables[1] is for decryption.
  111. *
  112. * The first 4 sub-tables are the expanded S-box with MixColumns. The
  113. * last (_tables[01][4]) is the S-box itself.
  114. *
  115. * @private
  116. */
  117. // if we have yet to precompute the S-box tables
  118. // do so now
  119. if (!aesTables) {
  120. aesTables = precompute();
  121. }
  122. // then make a copy of that object for use
  123. this._tables = [[aesTables[0][0].slice(), aesTables[0][1].slice(), aesTables[0][2].slice(), aesTables[0][3].slice(), aesTables[0][4].slice()], [aesTables[1][0].slice(), aesTables[1][1].slice(), aesTables[1][2].slice(), aesTables[1][3].slice(), aesTables[1][4].slice()]];
  124. var i = undefined;
  125. var j = undefined;
  126. var tmp = undefined;
  127. var encKey = undefined;
  128. var decKey = undefined;
  129. var sbox = this._tables[0][4];
  130. var decTable = this._tables[1];
  131. var keyLen = key.length;
  132. var rcon = 1;
  133. if (keyLen !== 4 && keyLen !== 6 && keyLen !== 8) {
  134. throw new Error('Invalid aes key size');
  135. }
  136. encKey = key.slice(0);
  137. decKey = [];
  138. this._key = [encKey, decKey];
  139. // schedule encryption keys
  140. for (i = keyLen; i < 4 * keyLen + 28; i++) {
  141. tmp = encKey[i - 1];
  142. // apply sbox
  143. if (i % keyLen === 0 || keyLen === 8 && i % keyLen === 4) {
  144. tmp = sbox[tmp >>> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255];
  145. // shift rows and add rcon
  146. if (i % keyLen === 0) {
  147. tmp = tmp << 8 ^ tmp >>> 24 ^ rcon << 24;
  148. rcon = rcon << 1 ^ (rcon >> 7) * 283;
  149. }
  150. }
  151. encKey[i] = encKey[i - keyLen] ^ tmp;
  152. }
  153. // schedule decryption keys
  154. for (j = 0; i; j++, i--) {
  155. tmp = encKey[j & 3 ? i : i - 4];
  156. if (i <= 4 || j < 4) {
  157. decKey[j] = tmp;
  158. } else {
  159. decKey[j] = decTable[0][sbox[tmp >>> 24]] ^ decTable[1][sbox[tmp >> 16 & 255]] ^ decTable[2][sbox[tmp >> 8 & 255]] ^ decTable[3][sbox[tmp & 255]];
  160. }
  161. }
  162. }
  163. /**
  164. * Decrypt 16 bytes, specified as four 32-bit words.
  165. *
  166. * @param {Number} encrypted0 the first word to decrypt
  167. * @param {Number} encrypted1 the second word to decrypt
  168. * @param {Number} encrypted2 the third word to decrypt
  169. * @param {Number} encrypted3 the fourth word to decrypt
  170. * @param {Int32Array} out the array to write the decrypted words
  171. * into
  172. * @param {Number} offset the offset into the output array to start
  173. * writing results
  174. * @return {Array} The plaintext.
  175. */
  176. _createClass(AES, [{
  177. key: 'decrypt',
  178. value: function decrypt(encrypted0, encrypted1, encrypted2, encrypted3, out, offset) {
  179. var key = this._key[1];
  180. // state variables a,b,c,d are loaded with pre-whitened data
  181. var a = encrypted0 ^ key[0];
  182. var b = encrypted3 ^ key[1];
  183. var c = encrypted2 ^ key[2];
  184. var d = encrypted1 ^ key[3];
  185. var a2 = undefined;
  186. var b2 = undefined;
  187. var c2 = undefined;
  188. // key.length === 2 ?
  189. var nInnerRounds = key.length / 4 - 2;
  190. var i = undefined;
  191. var kIndex = 4;
  192. var table = this._tables[1];
  193. // load up the tables
  194. var table0 = table[0];
  195. var table1 = table[1];
  196. var table2 = table[2];
  197. var table3 = table[3];
  198. var sbox = table[4];
  199. // Inner rounds. Cribbed from OpenSSL.
  200. for (i = 0; i < nInnerRounds; i++) {
  201. a2 = table0[a >>> 24] ^ table1[b >> 16 & 255] ^ table2[c >> 8 & 255] ^ table3[d & 255] ^ key[kIndex];
  202. b2 = table0[b >>> 24] ^ table1[c >> 16 & 255] ^ table2[d >> 8 & 255] ^ table3[a & 255] ^ key[kIndex + 1];
  203. c2 = table0[c >>> 24] ^ table1[d >> 16 & 255] ^ table2[a >> 8 & 255] ^ table3[b & 255] ^ key[kIndex + 2];
  204. d = table0[d >>> 24] ^ table1[a >> 16 & 255] ^ table2[b >> 8 & 255] ^ table3[c & 255] ^ key[kIndex + 3];
  205. kIndex += 4;
  206. a = a2;b = b2;c = c2;
  207. }
  208. // Last round.
  209. for (i = 0; i < 4; i++) {
  210. out[(3 & -i) + offset] = sbox[a >>> 24] << 24 ^ sbox[b >> 16 & 255] << 16 ^ sbox[c >> 8 & 255] << 8 ^ sbox[d & 255] ^ key[kIndex++];
  211. a2 = a;a = b;b = c;c = d;d = a2;
  212. }
  213. }
  214. }]);
  215. return AES;
  216. })();
  217. exports['default'] = AES;
  218. module.exports = exports['default'];
  219. },{}],2:[function(require,module,exports){
  220. /**
  221. * @file async-stream.js
  222. */
  223. 'use strict';
  224. Object.defineProperty(exports, '__esModule', {
  225. value: true
  226. });
  227. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  228. var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
  229. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  230. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  231. 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; }
  232. var _stream = require('./stream');
  233. var _stream2 = _interopRequireDefault(_stream);
  234. /**
  235. * A wrapper around the Stream class to use setTiemout
  236. * and run stream "jobs" Asynchronously
  237. *
  238. * @class AsyncStream
  239. * @extends Stream
  240. */
  241. var AsyncStream = (function (_Stream) {
  242. _inherits(AsyncStream, _Stream);
  243. function AsyncStream() {
  244. _classCallCheck(this, AsyncStream);
  245. _get(Object.getPrototypeOf(AsyncStream.prototype), 'constructor', this).call(this, _stream2['default']);
  246. this.jobs = [];
  247. this.delay = 1;
  248. this.timeout_ = null;
  249. }
  250. /**
  251. * process an async job
  252. *
  253. * @private
  254. */
  255. _createClass(AsyncStream, [{
  256. key: 'processJob_',
  257. value: function processJob_() {
  258. this.jobs.shift()();
  259. if (this.jobs.length) {
  260. this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
  261. } else {
  262. this.timeout_ = null;
  263. }
  264. }
  265. /**
  266. * push a job into the stream
  267. *
  268. * @param {Function} job the job to push into the stream
  269. */
  270. }, {
  271. key: 'push',
  272. value: function push(job) {
  273. this.jobs.push(job);
  274. if (!this.timeout_) {
  275. this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
  276. }
  277. }
  278. }]);
  279. return AsyncStream;
  280. })(_stream2['default']);
  281. exports['default'] = AsyncStream;
  282. module.exports = exports['default'];
  283. },{"./stream":4}],3:[function(require,module,exports){
  284. /**
  285. * @file decrypter.js
  286. *
  287. * An asynchronous implementation of AES-128 CBC decryption with
  288. * PKCS#7 padding.
  289. */
  290. 'use strict';
  291. Object.defineProperty(exports, '__esModule', {
  292. value: true
  293. });
  294. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  295. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  296. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  297. var _aes = require('./aes');
  298. var _aes2 = _interopRequireDefault(_aes);
  299. var _asyncStream = require('./async-stream');
  300. var _asyncStream2 = _interopRequireDefault(_asyncStream);
  301. var _pkcs7 = require('pkcs7');
  302. /**
  303. * Convert network-order (big-endian) bytes into their little-endian
  304. * representation.
  305. */
  306. var ntoh = function ntoh(word) {
  307. return word << 24 | (word & 0xff00) << 8 | (word & 0xff0000) >> 8 | word >>> 24;
  308. };
  309. /**
  310. * Decrypt bytes using AES-128 with CBC and PKCS#7 padding.
  311. *
  312. * @param {Uint8Array} encrypted the encrypted bytes
  313. * @param {Uint32Array} key the bytes of the decryption key
  314. * @param {Uint32Array} initVector the initialization vector (IV) to
  315. * use for the first round of CBC.
  316. * @return {Uint8Array} the decrypted bytes
  317. *
  318. * @see http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
  319. * @see http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Block_Chaining_.28CBC.29
  320. * @see https://tools.ietf.org/html/rfc2315
  321. */
  322. var decrypt = function decrypt(encrypted, key, initVector) {
  323. // word-level access to the encrypted bytes
  324. var encrypted32 = new Int32Array(encrypted.buffer, encrypted.byteOffset, encrypted.byteLength >> 2);
  325. var decipher = new _aes2['default'](Array.prototype.slice.call(key));
  326. // byte and word-level access for the decrypted output
  327. var decrypted = new Uint8Array(encrypted.byteLength);
  328. var decrypted32 = new Int32Array(decrypted.buffer);
  329. // temporary variables for working with the IV, encrypted, and
  330. // decrypted data
  331. var init0 = undefined;
  332. var init1 = undefined;
  333. var init2 = undefined;
  334. var init3 = undefined;
  335. var encrypted0 = undefined;
  336. var encrypted1 = undefined;
  337. var encrypted2 = undefined;
  338. var encrypted3 = undefined;
  339. // iteration variable
  340. var wordIx = undefined;
  341. // pull out the words of the IV to ensure we don't modify the
  342. // passed-in reference and easier access
  343. init0 = initVector[0];
  344. init1 = initVector[1];
  345. init2 = initVector[2];
  346. init3 = initVector[3];
  347. // decrypt four word sequences, applying cipher-block chaining (CBC)
  348. // to each decrypted block
  349. for (wordIx = 0; wordIx < encrypted32.length; wordIx += 4) {
  350. // convert big-endian (network order) words into little-endian
  351. // (javascript order)
  352. encrypted0 = ntoh(encrypted32[wordIx]);
  353. encrypted1 = ntoh(encrypted32[wordIx + 1]);
  354. encrypted2 = ntoh(encrypted32[wordIx + 2]);
  355. encrypted3 = ntoh(encrypted32[wordIx + 3]);
  356. // decrypt the block
  357. decipher.decrypt(encrypted0, encrypted1, encrypted2, encrypted3, decrypted32, wordIx);
  358. // XOR with the IV, and restore network byte-order to obtain the
  359. // plaintext
  360. decrypted32[wordIx] = ntoh(decrypted32[wordIx] ^ init0);
  361. decrypted32[wordIx + 1] = ntoh(decrypted32[wordIx + 1] ^ init1);
  362. decrypted32[wordIx + 2] = ntoh(decrypted32[wordIx + 2] ^ init2);
  363. decrypted32[wordIx + 3] = ntoh(decrypted32[wordIx + 3] ^ init3);
  364. // setup the IV for the next round
  365. init0 = encrypted0;
  366. init1 = encrypted1;
  367. init2 = encrypted2;
  368. init3 = encrypted3;
  369. }
  370. return decrypted;
  371. };
  372. exports.decrypt = decrypt;
  373. /**
  374. * The `Decrypter` class that manages decryption of AES
  375. * data through `AsyncStream` objects and the `decrypt`
  376. * function
  377. *
  378. * @param {Uint8Array} encrypted the encrypted bytes
  379. * @param {Uint32Array} key the bytes of the decryption key
  380. * @param {Uint32Array} initVector the initialization vector (IV) to
  381. * @param {Function} done the function to run when done
  382. * @class Decrypter
  383. */
  384. var Decrypter = (function () {
  385. function Decrypter(encrypted, key, initVector, done) {
  386. _classCallCheck(this, Decrypter);
  387. var step = Decrypter.STEP;
  388. var encrypted32 = new Int32Array(encrypted.buffer);
  389. var decrypted = new Uint8Array(encrypted.byteLength);
  390. var i = 0;
  391. this.asyncStream_ = new _asyncStream2['default']();
  392. // split up the encryption job and do the individual chunks asynchronously
  393. this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
  394. for (i = step; i < encrypted32.length; i += step) {
  395. initVector = new Uint32Array([ntoh(encrypted32[i - 4]), ntoh(encrypted32[i - 3]), ntoh(encrypted32[i - 2]), ntoh(encrypted32[i - 1])]);
  396. this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
  397. }
  398. // invoke the done() callback when everything is finished
  399. this.asyncStream_.push(function () {
  400. // remove pkcs#7 padding from the decrypted bytes
  401. done(null, (0, _pkcs7.unpad)(decrypted));
  402. });
  403. }
  404. /**
  405. * a getter for step the maximum number of bytes to process at one time
  406. *
  407. * @return {Number} the value of step 32000
  408. */
  409. _createClass(Decrypter, [{
  410. key: 'decryptChunk_',
  411. /**
  412. * @private
  413. */
  414. value: function decryptChunk_(encrypted, key, initVector, decrypted) {
  415. return function () {
  416. var bytes = decrypt(encrypted, key, initVector);
  417. decrypted.set(bytes, encrypted.byteOffset);
  418. };
  419. }
  420. }], [{
  421. key: 'STEP',
  422. get: function get() {
  423. // 4 * 8000;
  424. return 32000;
  425. }
  426. }]);
  427. return Decrypter;
  428. })();
  429. exports.Decrypter = Decrypter;
  430. exports['default'] = {
  431. Decrypter: Decrypter,
  432. decrypt: decrypt
  433. };
  434. },{"./aes":1,"./async-stream":2,"pkcs7":6}],4:[function(require,module,exports){
  435. /**
  436. * @file stream.js
  437. */
  438. /**
  439. * A lightweight readable stream implemention that handles event dispatching.
  440. *
  441. * @class Stream
  442. */
  443. 'use strict';
  444. Object.defineProperty(exports, '__esModule', {
  445. value: true
  446. });
  447. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  448. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  449. var Stream = (function () {
  450. function Stream() {
  451. _classCallCheck(this, Stream);
  452. this.listeners = {};
  453. }
  454. /**
  455. * Add a listener for a specified event type.
  456. *
  457. * @param {String} type the event name
  458. * @param {Function} listener the callback to be invoked when an event of
  459. * the specified type occurs
  460. */
  461. _createClass(Stream, [{
  462. key: 'on',
  463. value: function on(type, listener) {
  464. if (!this.listeners[type]) {
  465. this.listeners[type] = [];
  466. }
  467. this.listeners[type].push(listener);
  468. }
  469. /**
  470. * Remove a listener for a specified event type.
  471. *
  472. * @param {String} type the event name
  473. * @param {Function} listener a function previously registered for this
  474. * type of event through `on`
  475. * @return {Boolean} if we could turn it off or not
  476. */
  477. }, {
  478. key: 'off',
  479. value: function off(type, listener) {
  480. var index = undefined;
  481. if (!this.listeners[type]) {
  482. return false;
  483. }
  484. index = this.listeners[type].indexOf(listener);
  485. this.listeners[type].splice(index, 1);
  486. return index > -1;
  487. }
  488. /**
  489. * Trigger an event of the specified type on this stream. Any additional
  490. * arguments to this function are passed as parameters to event listeners.
  491. *
  492. * @param {String} type the event name
  493. */
  494. }, {
  495. key: 'trigger',
  496. value: function trigger(type) {
  497. var callbacks = undefined;
  498. var i = undefined;
  499. var length = undefined;
  500. var args = undefined;
  501. callbacks = this.listeners[type];
  502. if (!callbacks) {
  503. return;
  504. }
  505. // Slicing the arguments on every invocation of this method
  506. // can add a significant amount of overhead. Avoid the
  507. // intermediate object creation for the common case of a
  508. // single callback argument
  509. if (arguments.length === 2) {
  510. length = callbacks.length;
  511. for (i = 0; i < length; ++i) {
  512. callbacks[i].call(this, arguments[1]);
  513. }
  514. } else {
  515. args = Array.prototype.slice.call(arguments, 1);
  516. length = callbacks.length;
  517. for (i = 0; i < length; ++i) {
  518. callbacks[i].apply(this, args);
  519. }
  520. }
  521. }
  522. /**
  523. * Destroys the stream and cleans up.
  524. */
  525. }, {
  526. key: 'dispose',
  527. value: function dispose() {
  528. this.listeners = {};
  529. }
  530. /**
  531. * Forwards all `data` events on this stream to the destination stream. The
  532. * destination stream should provide a method `push` to receive the data
  533. * events as they arrive.
  534. *
  535. * @param {Stream} destination the stream that will receive all `data` events
  536. * @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
  537. */
  538. }, {
  539. key: 'pipe',
  540. value: function pipe(destination) {
  541. this.on('data', function (data) {
  542. destination.push(data);
  543. });
  544. }
  545. }]);
  546. return Stream;
  547. })();
  548. exports['default'] = Stream;
  549. module.exports = exports['default'];
  550. },{}],5:[function(require,module,exports){
  551. /*
  552. * pkcs7.pad
  553. * https://github.com/brightcove/pkcs7
  554. *
  555. * Copyright (c) 2014 Brightcove
  556. * Licensed under the apache2 license.
  557. */
  558. 'use strict';
  559. var PADDING;
  560. /**
  561. * Returns a new Uint8Array that is padded with PKCS#7 padding.
  562. * @param plaintext {Uint8Array} the input bytes before encryption
  563. * @return {Uint8Array} the padded bytes
  564. * @see http://tools.ietf.org/html/rfc5652
  565. */
  566. module.exports = function pad(plaintext) {
  567. var padding = PADDING[(plaintext.byteLength % 16) || 0],
  568. result = new Uint8Array(plaintext.byteLength + padding.length);
  569. result.set(plaintext);
  570. result.set(padding, plaintext.byteLength);
  571. return result;
  572. };
  573. // pre-define the padding values
  574. PADDING = [
  575. [16, 16, 16, 16,
  576. 16, 16, 16, 16,
  577. 16, 16, 16, 16,
  578. 16, 16, 16, 16],
  579. [15, 15, 15, 15,
  580. 15, 15, 15, 15,
  581. 15, 15, 15, 15,
  582. 15, 15, 15],
  583. [14, 14, 14, 14,
  584. 14, 14, 14, 14,
  585. 14, 14, 14, 14,
  586. 14, 14],
  587. [13, 13, 13, 13,
  588. 13, 13, 13, 13,
  589. 13, 13, 13, 13,
  590. 13],
  591. [12, 12, 12, 12,
  592. 12, 12, 12, 12,
  593. 12, 12, 12, 12],
  594. [11, 11, 11, 11,
  595. 11, 11, 11, 11,
  596. 11, 11, 11],
  597. [10, 10, 10, 10,
  598. 10, 10, 10, 10,
  599. 10, 10],
  600. [9, 9, 9, 9,
  601. 9, 9, 9, 9,
  602. 9],
  603. [8, 8, 8, 8,
  604. 8, 8, 8, 8],
  605. [7, 7, 7, 7,
  606. 7, 7, 7],
  607. [6, 6, 6, 6,
  608. 6, 6],
  609. [5, 5, 5, 5,
  610. 5],
  611. [4, 4, 4, 4],
  612. [3, 3, 3],
  613. [2, 2],
  614. [1]
  615. ];
  616. },{}],6:[function(require,module,exports){
  617. /*
  618. * pkcs7
  619. * https://github.com/brightcove/pkcs7
  620. *
  621. * Copyright (c) 2014 Brightcove
  622. * Licensed under the apache2 license.
  623. */
  624. 'use strict';
  625. exports.pad = require('./pad.js');
  626. exports.unpad = require('./unpad.js');
  627. },{"./pad.js":5,"./unpad.js":7}],7:[function(require,module,exports){
  628. /*
  629. * pkcs7.unpad
  630. * https://github.com/brightcove/pkcs7
  631. *
  632. * Copyright (c) 2014 Brightcove
  633. * Licensed under the apache2 license.
  634. */
  635. 'use strict';
  636. /**
  637. * Returns the subarray of a Uint8Array without PKCS#7 padding.
  638. * @param padded {Uint8Array} unencrypted bytes that have been padded
  639. * @return {Uint8Array} the unpadded bytes
  640. * @see http://tools.ietf.org/html/rfc5652
  641. */
  642. module.exports = function unpad(padded) {
  643. return padded.subarray(0, padded.byteLength - padded[padded.byteLength - 1]);
  644. };
  645. },{}],8:[function(require,module,exports){
  646. /**
  647. * @file index.js
  648. *
  649. * Index module to easily import the primary components of AES-128
  650. * decryption. Like this:
  651. *
  652. * ```js
  653. * import {Decrypter, decrypt, AsyncStream} from 'aes-decrypter';
  654. * ```
  655. */
  656. 'use strict';
  657. Object.defineProperty(exports, '__esModule', {
  658. value: true
  659. });
  660. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  661. var _decrypter = require('./decrypter');
  662. var _asyncStream = require('./async-stream');
  663. var _asyncStream2 = _interopRequireDefault(_asyncStream);
  664. exports['default'] = {
  665. decrypt: _decrypter.decrypt,
  666. Decrypter: _decrypter.Decrypter,
  667. AsyncStream: _asyncStream2['default']
  668. };
  669. module.exports = exports['default'];
  670. },{"./async-stream":2,"./decrypter":3}]},{},[8])(8)
  671. });