hasher.js


SUBMITTED BY: shopnuvem

DATE: May 19, 2017, 4:01 p.m.

FORMAT: Text only

SIZE: 6.5 kB

HITS: 1963

  1. function Hasher() {
  2. var self = this;
  3. this.B = []; // byte
  4. for(var i=0; i<(128+4); this.B[i++]=0) {}
  5. this.X = []; // integer
  6. for(var i=0; i<32; this.X[i++]=0) {}
  7. this.V = []; // integer
  8. for(var i=0; i<(32 * 1024); this.V[i++]=0) {}
  9. this.hash = function(header, nonce) {
  10. var i = 0;
  11. var j = 0;
  12. var k = 0;
  13. for(i=0; i<76; i++) {
  14. self.B[i] = header[i]; //arraycopy(header, 0, B, 0, 76);
  15. }
  16. self.B[76] = nonce >> 0;
  17. self.B[77] = nonce >> 8;
  18. self.B[78] = nonce >> 16;
  19. self.B[79] = nonce >> 24;
  20. var secretKey = CryptoJS.enc.Hex.parse(self.byteArrayToHexString(self.B.slice(0,80)));
  21. var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secretKey);
  22. self.B[80] = 0;
  23. self.B[81] = 0;
  24. self.B[82] = 0;
  25. for (i = 0; i < 4; i++) {
  26. self.B[83] = (i + 1);
  27. hmac.update(CryptoJS.enc.Hex.parse(self.byteArrayToHexString(self.B.slice(0,84))));
  28. var H = self.hexStringToByteArray((hmac.finalize()).toString(CryptoJS.enc.Hex));
  29. for (j = 0; j < 8; j++) {
  30. self.X[i * 8 + j] = (H[j * 4 + 0] & 0xff) << 0
  31. | (H[j * 4 + 1] & 0xff) << 8
  32. | (H[j * 4 + 2] & 0xff) << 16
  33. | (H[j * 4 + 3] & 0xff) << 24;
  34. }
  35. hmac.reset();
  36. }
  37. for (i = 0; i < 1024; i++) {
  38. for(j = 0; j < 32; j++) {
  39. self.V[(i*32)+j] = self.X[j]; // arraycopy(X, 0, V, i * 32, 32);
  40. }
  41. self.xorSalsa8(0, 16);
  42. self.xorSalsa8(16, 0);
  43. }
  44. for (i = 0; i < 1024; i++) {
  45. k = (self.X[16] & 1023) * 32;
  46. for (j = 0; j < 32; j++) {
  47. self.X[j] ^= self.V[k + j];
  48. }
  49. self.xorSalsa8(0, 16);
  50. self.xorSalsa8(16, 0);
  51. }
  52. for (i = 0; i < 32; i++) {
  53. self.B[i * 4 + 0] = (self.X[i] >> 0);
  54. self.B[i * 4 + 1] = (self.X[i] >> 8);
  55. self.B[i * 4 + 2] = (self.X[i] >> 16);
  56. self.B[i * 4 + 3] = (self.X[i] >> 24);
  57. }
  58. self.B[128 + 3] = 1;
  59. hmac.update(CryptoJS.enc.Hex.parse(self.byteArrayToHexString(self.B)));
  60. return self.hexStringToByteArray((hmac.finalize()).toString(CryptoJS.enc.Hex));
  61. };
  62. this.xorSalsa8 = function(di, xi) {
  63. var x00 = (self.X[di + 0] ^= self.X[xi + 0]);
  64. var x01 = (self.X[di + 1] ^= self.X[xi + 1]);
  65. var x02 = (self.X[di + 2] ^= self.X[xi + 2]);
  66. var x03 = (self.X[di + 3] ^= self.X[xi + 3]);
  67. var x04 = (self.X[di + 4] ^= self.X[xi + 4]);
  68. var x05 = (self.X[di + 5] ^= self.X[xi + 5]);
  69. var x06 = (self.X[di + 6] ^= self.X[xi + 6]);
  70. var x07 = (self.X[di + 7] ^= self.X[xi + 7]);
  71. var x08 = (self.X[di + 8] ^= self.X[xi + 8]);
  72. var x09 = (self.X[di + 9] ^= self.X[xi + 9]);
  73. var x10 = (self.X[di + 10] ^= self.X[xi + 10]);
  74. var x11 = (self.X[di + 11] ^= self.X[xi + 11]);
  75. var x12 = (self.X[di + 12] ^= self.X[xi + 12]);
  76. var x13 = (self.X[di + 13] ^= self.X[xi + 13]);
  77. var x14 = (self.X[di + 14] ^= self.X[xi + 14]);
  78. var x15 = (self.X[di + 15] ^= self.X[xi + 15]);
  79. for (var i = 0; i < 8; i += 2) {
  80. x04 ^= self.rotateLeft(x00+x12, 7); x08 ^= self.rotateLeft(x04+x00, 9);
  81. x12 ^= self.rotateLeft(x08+x04,13); x00 ^= self.rotateLeft(x12+x08,18);
  82. x09 ^= self.rotateLeft(x05+x01, 7); x13 ^= self.rotateLeft(x09+x05, 9);
  83. x01 ^= self.rotateLeft(x13+x09,13); x05 ^= self.rotateLeft(x01+x13,18);
  84. x14 ^= self.rotateLeft(x10+x06, 7); x02 ^= self.rotateLeft(x14+x10, 9);
  85. x06 ^= self.rotateLeft(x02+x14,13); x10 ^= self.rotateLeft(x06+x02,18);
  86. x03 ^= self.rotateLeft(x15+x11, 7); x07 ^= self.rotateLeft(x03+x15, 9);
  87. x11 ^= self.rotateLeft(x07+x03,13); x15 ^= self.rotateLeft(x11+x07,18);
  88. x01 ^= self.rotateLeft(x00+x03, 7); x02 ^= self.rotateLeft(x01+x00, 9);
  89. x03 ^= self.rotateLeft(x02+x01,13); x00 ^= self.rotateLeft(x03+x02,18);
  90. x06 ^= self.rotateLeft(x05+x04, 7); x07 ^= self.rotateLeft(x06+x05, 9);
  91. x04 ^= self.rotateLeft(x07+x06,13); x05 ^= self.rotateLeft(x04+x07,18);
  92. x11 ^= self.rotateLeft(x10+x09, 7); x08 ^= self.rotateLeft(x11+x10, 9);
  93. x09 ^= self.rotateLeft(x08+x11,13); x10 ^= self.rotateLeft(x09+x08,18);
  94. x12 ^= self.rotateLeft(x15+x14, 7); x13 ^= self.rotateLeft(x12+x15, 9);
  95. x14 ^= self.rotateLeft(x13+x12,13); x15 ^= self.rotateLeft(x14+x13,18);
  96. }
  97. self.X[di + 0] = (self.X[di + 0] + x00) | 0;
  98. self.X[di + 1] = (self.X[di + 1] + x01) | 0;
  99. self.X[di + 2] = (self.X[di + 2] + x02) | 0;
  100. self.X[di + 3] = (self.X[di + 3] + x03) | 0;
  101. self.X[di + 4] = (self.X[di + 4] + x04) | 0;
  102. self.X[di + 5] = (self.X[di + 5] + x05) | 0;
  103. self.X[di + 6] = (self.X[di + 6] + x06) | 0;
  104. self.X[di + 7] = (self.X[di + 7] + x07) | 0;
  105. self.X[di + 8] = (self.X[di + 8] + x08) | 0;
  106. self.X[di + 9] = (self.X[di + 9] + x09) | 0;
  107. self.X[di + 10] = (self.X[di + 10] + x10) | 0;
  108. self.X[di + 11] = (self.X[di + 11] + x11) | 0;
  109. self.X[di + 12] = (self.X[di + 12] + x12) | 0;
  110. self.X[di + 13] = (self.X[di + 13] + x13) | 0;
  111. self.X[di + 14] = (self.X[di + 14] + x14) | 0;
  112. self.X[di + 15] = (self.X[di + 15] + x15) | 0;
  113. };
  114. this.rotateLeft = function(i, distance) {
  115. return (i << distance) | (i >>> -distance);
  116. };
  117. this.byteArrayToHexString = function(b) {
  118. var sb = [];
  119. for (var i = 0; i < b.length; i++) {
  120. sb.push(((b[i] & 0xff) + 0x100).toString(16).substring(1));
  121. }
  122. return sb.join("");
  123. };
  124. this.hexStringToByteArray = function(s) {
  125. var len = s.length;
  126. var data = [];
  127. for(var i=0; i<(len/2); data[i++]=0) {}
  128. for (var i = 0; i < len; i += 2) {
  129. data[i / 2] = (parseInt(s.charAt(i), 16) << 4)
  130. + parseInt(s.charAt(i+1), 16);
  131. }
  132. return data;
  133. };
  134. };

comments powered by Disqus