_md.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.HashMD = exports.Maj = exports.Chi = void 0;
  4. const _assert_js_1 = require("./_assert.js");
  5. const utils_js_1 = require("./utils.js");
  6. // Polyfill for Safari 14
  7. function setBigUint64(view, byteOffset, value, isLE) {
  8. if (typeof view.setBigUint64 === 'function')
  9. return view.setBigUint64(byteOffset, value, isLE);
  10. const _32n = BigInt(32);
  11. const _u32_max = BigInt(0xffffffff);
  12. const wh = Number((value >> _32n) & _u32_max);
  13. const wl = Number(value & _u32_max);
  14. const h = isLE ? 4 : 0;
  15. const l = isLE ? 0 : 4;
  16. view.setUint32(byteOffset + h, wh, isLE);
  17. view.setUint32(byteOffset + l, wl, isLE);
  18. }
  19. // Choice: a ? b : c
  20. const Chi = (a, b, c) => (a & b) ^ (~a & c);
  21. exports.Chi = Chi;
  22. // Majority function, true if any two inpust is true
  23. const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
  24. exports.Maj = Maj;
  25. /**
  26. * Merkle-Damgard hash construction base class.
  27. * Could be used to create MD5, RIPEMD, SHA1, SHA2.
  28. */
  29. class HashMD extends utils_js_1.Hash {
  30. constructor(blockLen, outputLen, padOffset, isLE) {
  31. super();
  32. this.blockLen = blockLen;
  33. this.outputLen = outputLen;
  34. this.padOffset = padOffset;
  35. this.isLE = isLE;
  36. this.finished = false;
  37. this.length = 0;
  38. this.pos = 0;
  39. this.destroyed = false;
  40. this.buffer = new Uint8Array(blockLen);
  41. this.view = (0, utils_js_1.createView)(this.buffer);
  42. }
  43. update(data) {
  44. (0, _assert_js_1.exists)(this);
  45. const { view, buffer, blockLen } = this;
  46. data = (0, utils_js_1.toBytes)(data);
  47. const len = data.length;
  48. for (let pos = 0; pos < len;) {
  49. const take = Math.min(blockLen - this.pos, len - pos);
  50. // Fast path: we have at least one block in input, cast it to view and process
  51. if (take === blockLen) {
  52. const dataView = (0, utils_js_1.createView)(data);
  53. for (; blockLen <= len - pos; pos += blockLen)
  54. this.process(dataView, pos);
  55. continue;
  56. }
  57. buffer.set(data.subarray(pos, pos + take), this.pos);
  58. this.pos += take;
  59. pos += take;
  60. if (this.pos === blockLen) {
  61. this.process(view, 0);
  62. this.pos = 0;
  63. }
  64. }
  65. this.length += data.length;
  66. this.roundClean();
  67. return this;
  68. }
  69. digestInto(out) {
  70. (0, _assert_js_1.exists)(this);
  71. (0, _assert_js_1.output)(out, this);
  72. this.finished = true;
  73. // Padding
  74. // We can avoid allocation of buffer for padding completely if it
  75. // was previously not allocated here. But it won't change performance.
  76. const { buffer, view, blockLen, isLE } = this;
  77. let { pos } = this;
  78. // append the bit '1' to the message
  79. buffer[pos++] = 0b10000000;
  80. this.buffer.subarray(pos).fill(0);
  81. // we have less than padOffset left in buffer, so we cannot put length in
  82. // current block, need process it and pad again
  83. if (this.padOffset > blockLen - pos) {
  84. this.process(view, 0);
  85. pos = 0;
  86. }
  87. // Pad until full block byte with zeros
  88. for (let i = pos; i < blockLen; i++)
  89. buffer[i] = 0;
  90. // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
  91. // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
  92. // So we just write lowest 64 bits of that value.
  93. setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
  94. this.process(view, 0);
  95. const oview = (0, utils_js_1.createView)(out);
  96. const len = this.outputLen;
  97. // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
  98. if (len % 4)
  99. throw new Error('_sha2: outputLen should be aligned to 32bit');
  100. const outLen = len / 4;
  101. const state = this.get();
  102. if (outLen > state.length)
  103. throw new Error('_sha2: outputLen bigger than state');
  104. for (let i = 0; i < outLen; i++)
  105. oview.setUint32(4 * i, state[i], isLE);
  106. }
  107. digest() {
  108. const { buffer, outputLen } = this;
  109. this.digestInto(buffer);
  110. const res = buffer.slice(0, outputLen);
  111. this.destroy();
  112. return res;
  113. }
  114. _cloneInto(to) {
  115. to || (to = new this.constructor());
  116. to.set(...this.get());
  117. const { blockLen, buffer, length, finished, destroyed, pos } = this;
  118. to.length = length;
  119. to.pos = pos;
  120. to.finished = finished;
  121. to.destroyed = destroyed;
  122. if (length % blockLen)
  123. to.buffer.set(buffer);
  124. return to;
  125. }
  126. }
  127. exports.HashMD = HashMD;
  128. //# sourceMappingURL=_md.js.map