256.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. var utils = require('../utils');
  3. var common = require('../common');
  4. var shaCommon = require('./common');
  5. var assert = require('minimalistic-assert');
  6. var sum32 = utils.sum32;
  7. var sum32_4 = utils.sum32_4;
  8. var sum32_5 = utils.sum32_5;
  9. var ch32 = shaCommon.ch32;
  10. var maj32 = shaCommon.maj32;
  11. var s0_256 = shaCommon.s0_256;
  12. var s1_256 = shaCommon.s1_256;
  13. var g0_256 = shaCommon.g0_256;
  14. var g1_256 = shaCommon.g1_256;
  15. var BlockHash = common.BlockHash;
  16. var sha256_K = [
  17. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  18. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  19. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  20. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  21. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  22. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  23. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  24. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  25. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  26. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  27. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  28. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  29. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  30. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  31. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  32. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  33. ];
  34. function SHA256() {
  35. if (!(this instanceof SHA256))
  36. return new SHA256();
  37. BlockHash.call(this);
  38. this.h = [
  39. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  40. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  41. ];
  42. this.k = sha256_K;
  43. this.W = new Array(64);
  44. }
  45. utils.inherits(SHA256, BlockHash);
  46. module.exports = SHA256;
  47. SHA256.blockSize = 512;
  48. SHA256.outSize = 256;
  49. SHA256.hmacStrength = 192;
  50. SHA256.padLength = 64;
  51. SHA256.prototype._update = function _update(msg, start) {
  52. var W = this.W;
  53. for (var i = 0; i < 16; i++)
  54. W[i] = msg[start + i];
  55. for (; i < W.length; i++)
  56. W[i] = sum32_4(g1_256(W[i - 2]), W[i - 7], g0_256(W[i - 15]), W[i - 16]);
  57. var a = this.h[0];
  58. var b = this.h[1];
  59. var c = this.h[2];
  60. var d = this.h[3];
  61. var e = this.h[4];
  62. var f = this.h[5];
  63. var g = this.h[6];
  64. var h = this.h[7];
  65. assert(this.k.length === W.length);
  66. for (i = 0; i < W.length; i++) {
  67. var T1 = sum32_5(h, s1_256(e), ch32(e, f, g), this.k[i], W[i]);
  68. var T2 = sum32(s0_256(a), maj32(a, b, c));
  69. h = g;
  70. g = f;
  71. f = e;
  72. e = sum32(d, T1);
  73. d = c;
  74. c = b;
  75. b = a;
  76. a = sum32(T1, T2);
  77. }
  78. this.h[0] = sum32(this.h[0], a);
  79. this.h[1] = sum32(this.h[1], b);
  80. this.h[2] = sum32(this.h[2], c);
  81. this.h[3] = sum32(this.h[3], d);
  82. this.h[4] = sum32(this.h[4], e);
  83. this.h[5] = sum32(this.h[5], f);
  84. this.h[6] = sum32(this.h[6], g);
  85. this.h[7] = sum32(this.h[7], h);
  86. };
  87. SHA256.prototype._digest = function digest(enc) {
  88. if (enc === 'hex')
  89. return utils.toHex32(this.h, 'big');
  90. else
  91. return utils.split32(this.h, 'big');
  92. };