_vim_

js

Feb 17th, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sha256(ascii) {
  2.     function rightRotate(value, amount) {
  3.         return (value >>> amount) | (value << (32 - amount));
  4.     }
  5.  
  6.     var mathPow = Math.pow;
  7.     var maxWord = mathPow(2, 32);
  8.     var lengthProperty = 'length';
  9.     var i, j; // Used as a counter across the whole file
  10.     var result = '';
  11.  
  12.     var words = [];
  13.     var asciiBitLength = ascii[lengthProperty] * 8;
  14.  
  15.     //* caching results is optional - remove/add slash from front of this line to toggle
  16.     // Initial hash value: first 32 bits of the fractional parts of the square roots of the first 8 primes
  17.     // (we actually calculate the first 64, but extra values are just ignored)
  18.     // var hash = (sha256.h = sha256.h || []);
  19.     // // Round constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes
  20.     // var k = (sha256.k = sha256.k || []);
  21.     // var primeCounter = k[lengthProperty];
  22.     var hash = [], k = [];
  23.     var primeCounter = 0;
  24.    
  25.     var isComposite = {};
  26.     for (var candidate = 2; primeCounter < 64; candidate++) {
  27.         if (!isComposite[candidate]) {
  28.             for (i = 0; i < 313; i += candidate) {
  29.                 isComposite[i] = candidate;
  30.             }
  31.             hash[primeCounter] = (mathPow(candidate, 0.5) * maxWord) | 0;
  32.             k[primeCounter++] = (mathPow(candidate, 1 / 3) * maxWord) | 0;
  33.         }
  34.     }
  35.     console.log('hash:', hash, 'iscom', isComposite);
  36.    
  37.     ascii += '\x80'; // Append '1' bit (plus zero padding)
  38.     while (ascii[lengthProperty] % 64 - 56) ascii += '\x00'; // More zero padding
  39.     for (i = 0; i < ascii[lengthProperty]; i++) {
  40.         j = ascii.charCodeAt(i);
  41.         if (j >> 8) return; // ASCII check: only accept characters in range 0-255
  42.         words[i >> 2] |= j << (((3 - i) % 4) * 8);
  43.     }
  44.     words[words[lengthProperty]] = (asciiBitLength / maxWord) | 0;
  45.     words[words[lengthProperty]] = asciiBitLength;
  46.  
  47.     // process each chunk
  48.     for (j = 0; j < words[lengthProperty]; ) {
  49.         var w = words.slice(j, (j += 16)); // The message is expanded into 64 words as part of the iteration
  50.         var oldHash = hash;
  51.         // This is now the "working hash", often labelled as variables a...g
  52.         // (we have to truncate as well, otherwise extra entries at the end accumulate
  53.         hash = hash.slice(0, 8);
  54.  
  55.         for (i = 0; i < 64; i++) {
  56.             var i2 = i + j;
  57.             // Expand the message into 64 words
  58.             // Used below if
  59.             var w15 = w[i - 15],
  60.                 w2 = w[i - 2];
  61.  
  62.             // Iterate
  63.             var a = hash[0],
  64.                 e = hash[4];
  65.             var temp1 =
  66.                 hash[7] +
  67.                 (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)) + // S1
  68.                 ((e & hash[5]) ^ (~e & hash[6])) + // ch
  69.                 k[i] +
  70.                 // Expand the message schedule if needed
  71.                 (w[i] =
  72.                     i < 16
  73.                         ? w[i]
  74.                         : (w[i - 16] +
  75.                             (rightRotate(w15, 7) ^ rightRotate(w15, 18) ^ (w15 >>> 3)) + // s0
  76.                                 w[i - 7] +
  77.                                 (rightRotate(w2, 17) ^ rightRotate(w2, 19) ^ (w2 >>> 10))) | // s1
  78.                             0);
  79.             // This is only used once, so *could* be moved below, but it only saves 4 bytes and makes things unreadble
  80.             var temp2 =
  81.                 (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)) + // S0
  82.                 ((a & hash[1]) ^ (a & hash[2]) ^ (hash[1] & hash[2])); // maj
  83.  
  84.             hash = [ (temp1 + temp2) | 0 ].concat(hash); // We don't bother trimming off the extra ones, they're harmless as long as we're truncating when we do the slice()
  85.             hash[4] = (hash[4] + temp1) | 0;
  86.         }
  87.  
  88.         for (i = 0; i < 8; i++) {
  89.             hash[i] = (hash[i] + oldHash[i]) | 0;
  90.         }
  91.     }
  92.  
  93.     for (i = 0; i < 8; i++) {
  94.         for (j = 3; j + 1; j--) {
  95.             var b = (hash[i] >> (j * 8)) & 255;
  96.             result += (b < 16 ? 0 : '') + b.toString(16);
  97.         }
  98.     }
  99.     console.log('hash:', hash);
  100.     return result;
  101. }
  102.  
  103. const digest = sha256('111');
  104. console.log(digest);
Add Comment
Please, Sign In to add comment