blake2.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.blake2s = exports.BLAKE2s = exports.blake2b = exports.BLAKE2b = exports.BLAKE2 = void 0;
  4. exports.compress = compress;
  5. /**
  6. * blake2b (64-bit) & blake2s (8 to 32-bit) hash functions.
  7. * b could have been faster, but there is no fast u64 in js, so s is 1.5x faster.
  8. * @module
  9. */
  10. const _blake_ts_1 = require("./_blake.js");
  11. const _md_ts_1 = require("./_md.js");
  12. const u64 = require("./_u64.js");
  13. // prettier-ignore
  14. const utils_ts_1 = require("./utils.js");
  15. // Same as SHA512_IV, but swapped endianness: LE instead of BE. iv[1] is iv[0], etc.
  16. const B2B_IV = /* @__PURE__ */ Uint32Array.from([
  17. 0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a,
  18. 0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19,
  19. ]);
  20. // Temporary buffer
  21. const BBUF = /* @__PURE__ */ new Uint32Array(32);
  22. // Mixing function G splitted in two halfs
  23. function G1b(a, b, c, d, msg, x) {
  24. // NOTE: V is LE here
  25. const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
  26. let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
  27. let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
  28. let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
  29. let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
  30. // v[a] = (v[a] + v[b] + x) | 0;
  31. let ll = u64.add3L(Al, Bl, Xl);
  32. Ah = u64.add3H(ll, Ah, Bh, Xh);
  33. Al = ll | 0;
  34. // v[d] = rotr(v[d] ^ v[a], 32)
  35. ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
  36. ({ Dh, Dl } = { Dh: u64.rotr32H(Dh, Dl), Dl: u64.rotr32L(Dh, Dl) });
  37. // v[c] = (v[c] + v[d]) | 0;
  38. ({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
  39. // v[b] = rotr(v[b] ^ v[c], 24)
  40. ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
  41. ({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 24), Bl: u64.rotrSL(Bh, Bl, 24) });
  42. (BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah);
  43. (BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh);
  44. (BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch);
  45. (BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh);
  46. }
  47. function G2b(a, b, c, d, msg, x) {
  48. // NOTE: V is LE here
  49. const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
  50. let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
  51. let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
  52. let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
  53. let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
  54. // v[a] = (v[a] + v[b] + x) | 0;
  55. let ll = u64.add3L(Al, Bl, Xl);
  56. Ah = u64.add3H(ll, Ah, Bh, Xh);
  57. Al = ll | 0;
  58. // v[d] = rotr(v[d] ^ v[a], 16)
  59. ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
  60. ({ Dh, Dl } = { Dh: u64.rotrSH(Dh, Dl, 16), Dl: u64.rotrSL(Dh, Dl, 16) });
  61. // v[c] = (v[c] + v[d]) | 0;
  62. ({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
  63. // v[b] = rotr(v[b] ^ v[c], 63)
  64. ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
  65. ({ Bh, Bl } = { Bh: u64.rotrBH(Bh, Bl, 63), Bl: u64.rotrBL(Bh, Bl, 63) });
  66. (BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah);
  67. (BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh);
  68. (BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch);
  69. (BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh);
  70. }
  71. function checkBlake2Opts(outputLen, opts = {}, keyLen, saltLen, persLen) {
  72. (0, utils_ts_1.anumber)(keyLen);
  73. if (outputLen < 0 || outputLen > keyLen)
  74. throw new Error('outputLen bigger than keyLen');
  75. const { key, salt, personalization } = opts;
  76. if (key !== undefined && (key.length < 1 || key.length > keyLen))
  77. throw new Error('key length must be undefined or 1..' + keyLen);
  78. if (salt !== undefined && salt.length !== saltLen)
  79. throw new Error('salt must be undefined or ' + saltLen);
  80. if (personalization !== undefined && personalization.length !== persLen)
  81. throw new Error('personalization must be undefined or ' + persLen);
  82. }
  83. /** Class, from which others are subclassed. */
  84. class BLAKE2 extends utils_ts_1.Hash {
  85. constructor(blockLen, outputLen) {
  86. super();
  87. this.finished = false;
  88. this.destroyed = false;
  89. this.length = 0;
  90. this.pos = 0;
  91. (0, utils_ts_1.anumber)(blockLen);
  92. (0, utils_ts_1.anumber)(outputLen);
  93. this.blockLen = blockLen;
  94. this.outputLen = outputLen;
  95. this.buffer = new Uint8Array(blockLen);
  96. this.buffer32 = (0, utils_ts_1.u32)(this.buffer);
  97. }
  98. update(data) {
  99. (0, utils_ts_1.aexists)(this);
  100. data = (0, utils_ts_1.toBytes)(data);
  101. (0, utils_ts_1.abytes)(data);
  102. // Main difference with other hashes: there is flag for last block,
  103. // so we cannot process current block before we know that there
  104. // is the next one. This significantly complicates logic and reduces ability
  105. // to do zero-copy processing
  106. const { blockLen, buffer, buffer32 } = this;
  107. const len = data.length;
  108. const offset = data.byteOffset;
  109. const buf = data.buffer;
  110. for (let pos = 0; pos < len;) {
  111. // If buffer is full and we still have input (don't process last block, same as blake2s)
  112. if (this.pos === blockLen) {
  113. (0, utils_ts_1.swap32IfBE)(buffer32);
  114. this.compress(buffer32, 0, false);
  115. (0, utils_ts_1.swap32IfBE)(buffer32);
  116. this.pos = 0;
  117. }
  118. const take = Math.min(blockLen - this.pos, len - pos);
  119. const dataOffset = offset + pos;
  120. // full block && aligned to 4 bytes && not last in input
  121. if (take === blockLen && !(dataOffset % 4) && pos + take < len) {
  122. const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4));
  123. (0, utils_ts_1.swap32IfBE)(data32);
  124. for (let pos32 = 0; pos + blockLen < len; pos32 += buffer32.length, pos += blockLen) {
  125. this.length += blockLen;
  126. this.compress(data32, pos32, false);
  127. }
  128. (0, utils_ts_1.swap32IfBE)(data32);
  129. continue;
  130. }
  131. buffer.set(data.subarray(pos, pos + take), this.pos);
  132. this.pos += take;
  133. this.length += take;
  134. pos += take;
  135. }
  136. return this;
  137. }
  138. digestInto(out) {
  139. (0, utils_ts_1.aexists)(this);
  140. (0, utils_ts_1.aoutput)(out, this);
  141. const { pos, buffer32 } = this;
  142. this.finished = true;
  143. // Padding
  144. (0, utils_ts_1.clean)(this.buffer.subarray(pos));
  145. (0, utils_ts_1.swap32IfBE)(buffer32);
  146. this.compress(buffer32, 0, true);
  147. (0, utils_ts_1.swap32IfBE)(buffer32);
  148. const out32 = (0, utils_ts_1.u32)(out);
  149. this.get().forEach((v, i) => (out32[i] = (0, utils_ts_1.swap8IfBE)(v)));
  150. }
  151. digest() {
  152. const { buffer, outputLen } = this;
  153. this.digestInto(buffer);
  154. const res = buffer.slice(0, outputLen);
  155. this.destroy();
  156. return res;
  157. }
  158. _cloneInto(to) {
  159. const { buffer, length, finished, destroyed, outputLen, pos } = this;
  160. to || (to = new this.constructor({ dkLen: outputLen }));
  161. to.set(...this.get());
  162. to.buffer.set(buffer);
  163. to.destroyed = destroyed;
  164. to.finished = finished;
  165. to.length = length;
  166. to.pos = pos;
  167. // @ts-ignore
  168. to.outputLen = outputLen;
  169. return to;
  170. }
  171. clone() {
  172. return this._cloneInto();
  173. }
  174. }
  175. exports.BLAKE2 = BLAKE2;
  176. class BLAKE2b extends BLAKE2 {
  177. constructor(opts = {}) {
  178. const olen = opts.dkLen === undefined ? 64 : opts.dkLen;
  179. super(128, olen);
  180. // Same as SHA-512, but LE
  181. this.v0l = B2B_IV[0] | 0;
  182. this.v0h = B2B_IV[1] | 0;
  183. this.v1l = B2B_IV[2] | 0;
  184. this.v1h = B2B_IV[3] | 0;
  185. this.v2l = B2B_IV[4] | 0;
  186. this.v2h = B2B_IV[5] | 0;
  187. this.v3l = B2B_IV[6] | 0;
  188. this.v3h = B2B_IV[7] | 0;
  189. this.v4l = B2B_IV[8] | 0;
  190. this.v4h = B2B_IV[9] | 0;
  191. this.v5l = B2B_IV[10] | 0;
  192. this.v5h = B2B_IV[11] | 0;
  193. this.v6l = B2B_IV[12] | 0;
  194. this.v6h = B2B_IV[13] | 0;
  195. this.v7l = B2B_IV[14] | 0;
  196. this.v7h = B2B_IV[15] | 0;
  197. checkBlake2Opts(olen, opts, 64, 16, 16);
  198. let { key, personalization, salt } = opts;
  199. let keyLength = 0;
  200. if (key !== undefined) {
  201. key = (0, utils_ts_1.toBytes)(key);
  202. keyLength = key.length;
  203. }
  204. this.v0l ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
  205. if (salt !== undefined) {
  206. salt = (0, utils_ts_1.toBytes)(salt);
  207. const slt = (0, utils_ts_1.u32)(salt);
  208. this.v4l ^= (0, utils_ts_1.swap8IfBE)(slt[0]);
  209. this.v4h ^= (0, utils_ts_1.swap8IfBE)(slt[1]);
  210. this.v5l ^= (0, utils_ts_1.swap8IfBE)(slt[2]);
  211. this.v5h ^= (0, utils_ts_1.swap8IfBE)(slt[3]);
  212. }
  213. if (personalization !== undefined) {
  214. personalization = (0, utils_ts_1.toBytes)(personalization);
  215. const pers = (0, utils_ts_1.u32)(personalization);
  216. this.v6l ^= (0, utils_ts_1.swap8IfBE)(pers[0]);
  217. this.v6h ^= (0, utils_ts_1.swap8IfBE)(pers[1]);
  218. this.v7l ^= (0, utils_ts_1.swap8IfBE)(pers[2]);
  219. this.v7h ^= (0, utils_ts_1.swap8IfBE)(pers[3]);
  220. }
  221. if (key !== undefined) {
  222. // Pad to blockLen and update
  223. const tmp = new Uint8Array(this.blockLen);
  224. tmp.set(key);
  225. this.update(tmp);
  226. }
  227. }
  228. // prettier-ignore
  229. get() {
  230. let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
  231. return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
  232. }
  233. // prettier-ignore
  234. set(v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h) {
  235. this.v0l = v0l | 0;
  236. this.v0h = v0h | 0;
  237. this.v1l = v1l | 0;
  238. this.v1h = v1h | 0;
  239. this.v2l = v2l | 0;
  240. this.v2h = v2h | 0;
  241. this.v3l = v3l | 0;
  242. this.v3h = v3h | 0;
  243. this.v4l = v4l | 0;
  244. this.v4h = v4h | 0;
  245. this.v5l = v5l | 0;
  246. this.v5h = v5h | 0;
  247. this.v6l = v6l | 0;
  248. this.v6h = v6h | 0;
  249. this.v7l = v7l | 0;
  250. this.v7h = v7h | 0;
  251. }
  252. compress(msg, offset, isLast) {
  253. this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
  254. BBUF.set(B2B_IV, 16); // Second half from IV.
  255. let { h, l } = u64.fromBig(BigInt(this.length));
  256. BBUF[24] = B2B_IV[8] ^ l; // Low word of the offset.
  257. BBUF[25] = B2B_IV[9] ^ h; // High word.
  258. // Invert all bits for last block
  259. if (isLast) {
  260. BBUF[28] = ~BBUF[28];
  261. BBUF[29] = ~BBUF[29];
  262. }
  263. let j = 0;
  264. const s = _blake_ts_1.BSIGMA;
  265. for (let i = 0; i < 12; i++) {
  266. G1b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
  267. G2b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
  268. G1b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
  269. G2b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
  270. G1b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
  271. G2b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
  272. G1b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
  273. G2b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
  274. G1b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
  275. G2b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
  276. G1b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
  277. G2b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
  278. G1b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
  279. G2b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
  280. G1b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
  281. G2b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
  282. }
  283. this.v0l ^= BBUF[0] ^ BBUF[16];
  284. this.v0h ^= BBUF[1] ^ BBUF[17];
  285. this.v1l ^= BBUF[2] ^ BBUF[18];
  286. this.v1h ^= BBUF[3] ^ BBUF[19];
  287. this.v2l ^= BBUF[4] ^ BBUF[20];
  288. this.v2h ^= BBUF[5] ^ BBUF[21];
  289. this.v3l ^= BBUF[6] ^ BBUF[22];
  290. this.v3h ^= BBUF[7] ^ BBUF[23];
  291. this.v4l ^= BBUF[8] ^ BBUF[24];
  292. this.v4h ^= BBUF[9] ^ BBUF[25];
  293. this.v5l ^= BBUF[10] ^ BBUF[26];
  294. this.v5h ^= BBUF[11] ^ BBUF[27];
  295. this.v6l ^= BBUF[12] ^ BBUF[28];
  296. this.v6h ^= BBUF[13] ^ BBUF[29];
  297. this.v7l ^= BBUF[14] ^ BBUF[30];
  298. this.v7h ^= BBUF[15] ^ BBUF[31];
  299. (0, utils_ts_1.clean)(BBUF);
  300. }
  301. destroy() {
  302. this.destroyed = true;
  303. (0, utils_ts_1.clean)(this.buffer32);
  304. this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  305. }
  306. }
  307. exports.BLAKE2b = BLAKE2b;
  308. /**
  309. * Blake2b hash function. 64-bit. 1.5x slower than blake2s in JS.
  310. * @param msg - message that would be hashed
  311. * @param opts - dkLen output length, key for MAC mode, salt, personalization
  312. */
  313. exports.blake2b = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE2b(opts));
  314. // prettier-ignore
  315. function compress(s, offset, msg, rounds, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) {
  316. let j = 0;
  317. for (let i = 0; i < rounds; i++) {
  318. ({ a: v0, b: v4, c: v8, d: v12 } = (0, _blake_ts_1.G1s)(v0, v4, v8, v12, msg[offset + s[j++]]));
  319. ({ a: v0, b: v4, c: v8, d: v12 } = (0, _blake_ts_1.G2s)(v0, v4, v8, v12, msg[offset + s[j++]]));
  320. ({ a: v1, b: v5, c: v9, d: v13 } = (0, _blake_ts_1.G1s)(v1, v5, v9, v13, msg[offset + s[j++]]));
  321. ({ a: v1, b: v5, c: v9, d: v13 } = (0, _blake_ts_1.G2s)(v1, v5, v9, v13, msg[offset + s[j++]]));
  322. ({ a: v2, b: v6, c: v10, d: v14 } = (0, _blake_ts_1.G1s)(v2, v6, v10, v14, msg[offset + s[j++]]));
  323. ({ a: v2, b: v6, c: v10, d: v14 } = (0, _blake_ts_1.G2s)(v2, v6, v10, v14, msg[offset + s[j++]]));
  324. ({ a: v3, b: v7, c: v11, d: v15 } = (0, _blake_ts_1.G1s)(v3, v7, v11, v15, msg[offset + s[j++]]));
  325. ({ a: v3, b: v7, c: v11, d: v15 } = (0, _blake_ts_1.G2s)(v3, v7, v11, v15, msg[offset + s[j++]]));
  326. ({ a: v0, b: v5, c: v10, d: v15 } = (0, _blake_ts_1.G1s)(v0, v5, v10, v15, msg[offset + s[j++]]));
  327. ({ a: v0, b: v5, c: v10, d: v15 } = (0, _blake_ts_1.G2s)(v0, v5, v10, v15, msg[offset + s[j++]]));
  328. ({ a: v1, b: v6, c: v11, d: v12 } = (0, _blake_ts_1.G1s)(v1, v6, v11, v12, msg[offset + s[j++]]));
  329. ({ a: v1, b: v6, c: v11, d: v12 } = (0, _blake_ts_1.G2s)(v1, v6, v11, v12, msg[offset + s[j++]]));
  330. ({ a: v2, b: v7, c: v8, d: v13 } = (0, _blake_ts_1.G1s)(v2, v7, v8, v13, msg[offset + s[j++]]));
  331. ({ a: v2, b: v7, c: v8, d: v13 } = (0, _blake_ts_1.G2s)(v2, v7, v8, v13, msg[offset + s[j++]]));
  332. ({ a: v3, b: v4, c: v9, d: v14 } = (0, _blake_ts_1.G1s)(v3, v4, v9, v14, msg[offset + s[j++]]));
  333. ({ a: v3, b: v4, c: v9, d: v14 } = (0, _blake_ts_1.G2s)(v3, v4, v9, v14, msg[offset + s[j++]]));
  334. }
  335. return { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 };
  336. }
  337. const B2S_IV = _md_ts_1.SHA256_IV;
  338. class BLAKE2s extends BLAKE2 {
  339. constructor(opts = {}) {
  340. const olen = opts.dkLen === undefined ? 32 : opts.dkLen;
  341. super(64, olen);
  342. // Internal state, same as SHA-256
  343. this.v0 = B2S_IV[0] | 0;
  344. this.v1 = B2S_IV[1] | 0;
  345. this.v2 = B2S_IV[2] | 0;
  346. this.v3 = B2S_IV[3] | 0;
  347. this.v4 = B2S_IV[4] | 0;
  348. this.v5 = B2S_IV[5] | 0;
  349. this.v6 = B2S_IV[6] | 0;
  350. this.v7 = B2S_IV[7] | 0;
  351. checkBlake2Opts(olen, opts, 32, 8, 8);
  352. let { key, personalization, salt } = opts;
  353. let keyLength = 0;
  354. if (key !== undefined) {
  355. key = (0, utils_ts_1.toBytes)(key);
  356. keyLength = key.length;
  357. }
  358. this.v0 ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
  359. if (salt !== undefined) {
  360. salt = (0, utils_ts_1.toBytes)(salt);
  361. const slt = (0, utils_ts_1.u32)(salt);
  362. this.v4 ^= (0, utils_ts_1.swap8IfBE)(slt[0]);
  363. this.v5 ^= (0, utils_ts_1.swap8IfBE)(slt[1]);
  364. }
  365. if (personalization !== undefined) {
  366. personalization = (0, utils_ts_1.toBytes)(personalization);
  367. const pers = (0, utils_ts_1.u32)(personalization);
  368. this.v6 ^= (0, utils_ts_1.swap8IfBE)(pers[0]);
  369. this.v7 ^= (0, utils_ts_1.swap8IfBE)(pers[1]);
  370. }
  371. if (key !== undefined) {
  372. // Pad to blockLen and update
  373. (0, utils_ts_1.abytes)(key);
  374. const tmp = new Uint8Array(this.blockLen);
  375. tmp.set(key);
  376. this.update(tmp);
  377. }
  378. }
  379. get() {
  380. const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
  381. return [v0, v1, v2, v3, v4, v5, v6, v7];
  382. }
  383. // prettier-ignore
  384. set(v0, v1, v2, v3, v4, v5, v6, v7) {
  385. this.v0 = v0 | 0;
  386. this.v1 = v1 | 0;
  387. this.v2 = v2 | 0;
  388. this.v3 = v3 | 0;
  389. this.v4 = v4 | 0;
  390. this.v5 = v5 | 0;
  391. this.v6 = v6 | 0;
  392. this.v7 = v7 | 0;
  393. }
  394. compress(msg, offset, isLast) {
  395. const { h, l } = u64.fromBig(BigInt(this.length));
  396. // prettier-ignore
  397. const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = compress(_blake_ts_1.BSIGMA, offset, msg, 10, this.v0, this.v1, this.v2, this.v3, this.v4, this.v5, this.v6, this.v7, B2S_IV[0], B2S_IV[1], B2S_IV[2], B2S_IV[3], l ^ B2S_IV[4], h ^ B2S_IV[5], isLast ? ~B2S_IV[6] : B2S_IV[6], B2S_IV[7]);
  398. this.v0 ^= v0 ^ v8;
  399. this.v1 ^= v1 ^ v9;
  400. this.v2 ^= v2 ^ v10;
  401. this.v3 ^= v3 ^ v11;
  402. this.v4 ^= v4 ^ v12;
  403. this.v5 ^= v5 ^ v13;
  404. this.v6 ^= v6 ^ v14;
  405. this.v7 ^= v7 ^ v15;
  406. }
  407. destroy() {
  408. this.destroyed = true;
  409. (0, utils_ts_1.clean)(this.buffer32);
  410. this.set(0, 0, 0, 0, 0, 0, 0, 0);
  411. }
  412. }
  413. exports.BLAKE2s = BLAKE2s;
  414. /**
  415. * Blake2s hash function. Focuses on 8-bit to 32-bit platforms. 1.5x faster than blake2b in JS.
  416. * @param msg - message that would be hashed
  417. * @param opts - dkLen output length, key for MAC mode, salt, personalization
  418. */
  419. exports.blake2s = (0, utils_ts_1.createOptHasher)((opts) => new BLAKE2s(opts));
  420. //# sourceMappingURL=blake2.js.map