sha512.js 8.1 KB

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