sha256.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
  3. * in FIPS 180-2
  4. * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
  5. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  6. *
  7. */
  8. var inherits = require('inherits')
  9. var Hash = require('./hash')
  10. var Buffer = require('safe-buffer').Buffer
  11. var K = [
  12. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  13. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  14. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  15. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  16. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  17. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  18. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  19. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  20. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  21. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  22. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  23. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  24. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  25. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  26. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  27. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
  28. ]
  29. var W = new Array(64)
  30. function Sha256 () {
  31. this.init()
  32. this._w = W // new Array(64)
  33. Hash.call(this, 64, 56)
  34. }
  35. inherits(Sha256, Hash)
  36. Sha256.prototype.init = function () {
  37. this._a = 0x6a09e667
  38. this._b = 0xbb67ae85
  39. this._c = 0x3c6ef372
  40. this._d = 0xa54ff53a
  41. this._e = 0x510e527f
  42. this._f = 0x9b05688c
  43. this._g = 0x1f83d9ab
  44. this._h = 0x5be0cd19
  45. return this
  46. }
  47. function ch (x, y, z) {
  48. return z ^ (x & (y ^ z))
  49. }
  50. function maj (x, y, z) {
  51. return (x & y) | (z & (x | y))
  52. }
  53. function sigma0 (x) {
  54. return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10)
  55. }
  56. function sigma1 (x) {
  57. return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7)
  58. }
  59. function gamma0 (x) {
  60. return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3)
  61. }
  62. function gamma1 (x) {
  63. return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10)
  64. }
  65. Sha256.prototype._update = function (M) {
  66. var W = this._w
  67. var a = this._a | 0
  68. var b = this._b | 0
  69. var c = this._c | 0
  70. var d = this._d | 0
  71. var e = this._e | 0
  72. var f = this._f | 0
  73. var g = this._g | 0
  74. var h = this._h | 0
  75. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  76. for (; i < 64; ++i) W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0
  77. for (var j = 0; j < 64; ++j) {
  78. var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + W[j]) | 0
  79. var T2 = (sigma0(a) + maj(a, b, c)) | 0
  80. h = g
  81. g = f
  82. f = e
  83. e = (d + T1) | 0
  84. d = c
  85. c = b
  86. b = a
  87. a = (T1 + T2) | 0
  88. }
  89. this._a = (a + this._a) | 0
  90. this._b = (b + this._b) | 0
  91. this._c = (c + this._c) | 0
  92. this._d = (d + this._d) | 0
  93. this._e = (e + this._e) | 0
  94. this._f = (f + this._f) | 0
  95. this._g = (g + this._g) | 0
  96. this._h = (h + this._h) | 0
  97. }
  98. Sha256.prototype._hash = function () {
  99. var H = Buffer.allocUnsafe(32)
  100. H.writeInt32BE(this._a, 0)
  101. H.writeInt32BE(this._b, 4)
  102. H.writeInt32BE(this._c, 8)
  103. H.writeInt32BE(this._d, 12)
  104. H.writeInt32BE(this._e, 16)
  105. H.writeInt32BE(this._f, 20)
  106. H.writeInt32BE(this._g, 24)
  107. H.writeInt32BE(this._h, 28)
  108. return H
  109. }
  110. module.exports = Sha256