sha512.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. var inherits = require('inherits')
  2. var Hash = require('./hash')
  3. var Buffer = require('safe-buffer').Buffer
  4. var K = [
  5. 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
  6. 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
  7. 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
  8. 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
  9. 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
  10. 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
  11. 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
  12. 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
  13. 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
  14. 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
  15. 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
  16. 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
  17. 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
  18. 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
  19. 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
  20. 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
  21. 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
  22. 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
  23. 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
  24. 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
  25. 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
  26. 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
  27. 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
  28. 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
  29. 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
  30. 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
  31. 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
  32. 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
  33. 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
  34. 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
  35. 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
  36. 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
  37. 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
  38. 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
  39. 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
  40. 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
  41. 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
  42. 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
  43. 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
  44. 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
  45. ]
  46. var W = new Array(160)
  47. function Sha512 () {
  48. this.init()
  49. this._w = W
  50. Hash.call(this, 128, 112)
  51. }
  52. inherits(Sha512, Hash)
  53. Sha512.prototype.init = function () {
  54. this._ah = 0x6a09e667
  55. this._bh = 0xbb67ae85
  56. this._ch = 0x3c6ef372
  57. this._dh = 0xa54ff53a
  58. this._eh = 0x510e527f
  59. this._fh = 0x9b05688c
  60. this._gh = 0x1f83d9ab
  61. this._hh = 0x5be0cd19
  62. this._al = 0xf3bcc908
  63. this._bl = 0x84caa73b
  64. this._cl = 0xfe94f82b
  65. this._dl = 0x5f1d36f1
  66. this._el = 0xade682d1
  67. this._fl = 0x2b3e6c1f
  68. this._gl = 0xfb41bd6b
  69. this._hl = 0x137e2179
  70. return this
  71. }
  72. function Ch (x, y, z) {
  73. return z ^ (x & (y ^ z))
  74. }
  75. function maj (x, y, z) {
  76. return (x & y) | (z & (x | y))
  77. }
  78. function sigma0 (x, xl) {
  79. return (x >>> 28 | xl << 4) ^ (xl >>> 2 | x << 30) ^ (xl >>> 7 | x << 25)
  80. }
  81. function sigma1 (x, xl) {
  82. return (x >>> 14 | xl << 18) ^ (x >>> 18 | xl << 14) ^ (xl >>> 9 | x << 23)
  83. }
  84. function Gamma0 (x, xl) {
  85. return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7)
  86. }
  87. function Gamma0l (x, xl) {
  88. return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7 | xl << 25)
  89. }
  90. function Gamma1 (x, xl) {
  91. return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6)
  92. }
  93. function Gamma1l (x, xl) {
  94. return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6 | xl << 26)
  95. }
  96. function getCarry (a, b) {
  97. return (a >>> 0) < (b >>> 0) ? 1 : 0
  98. }
  99. Sha512.prototype._update = function (M) {
  100. var W = this._w
  101. var ah = this._ah | 0
  102. var bh = this._bh | 0
  103. var ch = this._ch | 0
  104. var dh = this._dh | 0
  105. var eh = this._eh | 0
  106. var fh = this._fh | 0
  107. var gh = this._gh | 0
  108. var hh = this._hh | 0
  109. var al = this._al | 0
  110. var bl = this._bl | 0
  111. var cl = this._cl | 0
  112. var dl = this._dl | 0
  113. var el = this._el | 0
  114. var fl = this._fl | 0
  115. var gl = this._gl | 0
  116. var hl = this._hl | 0
  117. for (var i = 0; i < 32; i += 2) {
  118. W[i] = M.readInt32BE(i * 4)
  119. W[i + 1] = M.readInt32BE(i * 4 + 4)
  120. }
  121. for (; i < 160; i += 2) {
  122. var xh = W[i - 15 * 2]
  123. var xl = W[i - 15 * 2 + 1]
  124. var gamma0 = Gamma0(xh, xl)
  125. var gamma0l = Gamma0l(xl, xh)
  126. xh = W[i - 2 * 2]
  127. xl = W[i - 2 * 2 + 1]
  128. var gamma1 = Gamma1(xh, xl)
  129. var gamma1l = Gamma1l(xl, xh)
  130. // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
  131. var Wi7h = W[i - 7 * 2]
  132. var Wi7l = W[i - 7 * 2 + 1]
  133. var Wi16h = W[i - 16 * 2]
  134. var Wi16l = W[i - 16 * 2 + 1]
  135. var Wil = (gamma0l + Wi7l) | 0
  136. var Wih = (gamma0 + Wi7h + getCarry(Wil, gamma0l)) | 0
  137. Wil = (Wil + gamma1l) | 0
  138. Wih = (Wih + gamma1 + getCarry(Wil, gamma1l)) | 0
  139. Wil = (Wil + Wi16l) | 0
  140. Wih = (Wih + Wi16h + getCarry(Wil, Wi16l)) | 0
  141. W[i] = Wih
  142. W[i + 1] = Wil
  143. }
  144. for (var j = 0; j < 160; j += 2) {
  145. Wih = W[j]
  146. Wil = W[j + 1]
  147. var majh = maj(ah, bh, ch)
  148. var majl = maj(al, bl, cl)
  149. var sigma0h = sigma0(ah, al)
  150. var sigma0l = sigma0(al, ah)
  151. var sigma1h = sigma1(eh, el)
  152. var sigma1l = sigma1(el, eh)
  153. // t1 = h + sigma1 + ch + K[j] + W[j]
  154. var Kih = K[j]
  155. var Kil = K[j + 1]
  156. var chh = Ch(eh, fh, gh)
  157. var chl = Ch(el, fl, gl)
  158. var t1l = (hl + sigma1l) | 0
  159. var t1h = (hh + sigma1h + getCarry(t1l, hl)) | 0
  160. t1l = (t1l + chl) | 0
  161. t1h = (t1h + chh + getCarry(t1l, chl)) | 0
  162. t1l = (t1l + Kil) | 0
  163. t1h = (t1h + Kih + getCarry(t1l, Kil)) | 0
  164. t1l = (t1l + Wil) | 0
  165. t1h = (t1h + Wih + getCarry(t1l, Wil)) | 0
  166. // t2 = sigma0 + maj
  167. var t2l = (sigma0l + majl) | 0
  168. var t2h = (sigma0h + majh + getCarry(t2l, sigma0l)) | 0
  169. hh = gh
  170. hl = gl
  171. gh = fh
  172. gl = fl
  173. fh = eh
  174. fl = el
  175. el = (dl + t1l) | 0
  176. eh = (dh + t1h + getCarry(el, dl)) | 0
  177. dh = ch
  178. dl = cl
  179. ch = bh
  180. cl = bl
  181. bh = ah
  182. bl = al
  183. al = (t1l + t2l) | 0
  184. ah = (t1h + t2h + getCarry(al, t1l)) | 0
  185. }
  186. this._al = (this._al + al) | 0
  187. this._bl = (this._bl + bl) | 0
  188. this._cl = (this._cl + cl) | 0
  189. this._dl = (this._dl + dl) | 0
  190. this._el = (this._el + el) | 0
  191. this._fl = (this._fl + fl) | 0
  192. this._gl = (this._gl + gl) | 0
  193. this._hl = (this._hl + hl) | 0
  194. this._ah = (this._ah + ah + getCarry(this._al, al)) | 0
  195. this._bh = (this._bh + bh + getCarry(this._bl, bl)) | 0
  196. this._ch = (this._ch + ch + getCarry(this._cl, cl)) | 0
  197. this._dh = (this._dh + dh + getCarry(this._dl, dl)) | 0
  198. this._eh = (this._eh + eh + getCarry(this._el, el)) | 0
  199. this._fh = (this._fh + fh + getCarry(this._fl, fl)) | 0
  200. this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0
  201. this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0
  202. }
  203. Sha512.prototype._hash = function () {
  204. var H = Buffer.allocUnsafe(64)
  205. function writeInt64BE (h, l, offset) {
  206. H.writeInt32BE(h, offset)
  207. H.writeInt32BE(l, offset + 4)
  208. }
  209. writeInt64BE(this._ah, this._al, 0)
  210. writeInt64BE(this._bh, this._bl, 8)
  211. writeInt64BE(this._ch, this._cl, 16)
  212. writeInt64BE(this._dh, this._dl, 24)
  213. writeInt64BE(this._eh, this._el, 32)
  214. writeInt64BE(this._fh, this._fl, 40)
  215. writeInt64BE(this._gh, this._gl, 48)
  216. writeInt64BE(this._hh, this._hl, 56)
  217. return H
  218. }
  219. module.exports = Sha512