_md.js 4.5 KB

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