1let test = crypto.createHmac('sha256', "key").update("json").digest("base64");
2
1async function sha256(message) {
2 // encode as UTF-8
3 const msgBuffer = new TextEncoder().encode(message);
4 // hash the message
5 const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
6 // convert ArrayBuffer to Array
7 const hashArray = Array.from(new Uint8Array(hashBuffer));
8 // convert bytes to hex string
9 const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('');
10 return hashHex;
11}
1var sha256 = 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 /*/
23 var hash = [], k = [];
24 var primeCounter = 0;
25 //*/
26
27 var isComposite = {};
28 for (var candidate = 2; primeCounter < 64; candidate++) {
29 if (!isComposite[candidate]) {
30 for (i = 0; i < 313; i += candidate) {
31 isComposite[i] = candidate;
32 }
33 hash[primeCounter] = (mathPow(candidate, .5)*maxWord)|0;
34 k[primeCounter++] = (mathPow(candidate, 1/3)*maxWord)|0;
35 }
36 }
37
38 ascii += '\x80' // Append Ƈ' bit (plus zero padding)
39 while (ascii[lengthProperty]%64 - 56) ascii += '\x00' // More zero padding
40 for (i = 0; i < ascii[lengthProperty]; i++) {
41 j = ascii.charCodeAt(i);
42 if (j>>8) return; // ASCII check: only accept characters in range 0-255
43 words[i>>2] |= j << ((3 - i)%4)*8;
44 }
45 words[words[lengthProperty]] = ((asciiBitLength/maxWord)|0);
46 words[words[lengthProperty]] = (asciiBitLength)
47
48 // process each chunk
49 for (j = 0; j < words[lengthProperty];) {
50 var w = words.slice(j, j += 16); // The message is expanded into 64 words as part of the iteration
51 var oldHash = hash;
52 // This is now the undefinedworking hash", often labelled as variables a...g
53 // (we have to truncate as well, otherwise extra entries at the end accumulate
54 hash = hash.slice(0, 8);
55
56 for (i = 0; i < 64; i++) {
57 var i2 = i + j;
58 // Expand the message into 64 words
59 // Used below if
60 var w15 = w[i - 15], w2 = w[i - 2];
61
62 // Iterate
63 var a = hash[0], e = hash[4];
64 var temp1 = hash[7]
65 + (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)) // S1
66 + ((e&hash[5])^((~e)&hash[6])) // ch
67 + k[i]
68 // Expand the message schedule if needed
69 + (w[i] = (i < 16) ? w[i] : (
70 w[i - 16]
71 + (rightRotate(w15, 7) ^ rightRotate(w15, 18) ^ (w15>>>3)) // s0
72 + w[i - 7]
73 + (rightRotate(w2, 17) ^ rightRotate(w2, 19) ^ (w2>>>10)) // s1
74 )|0
75 );
76 // This is only used once, so *could* be moved below, but it only saves 4 bytes and makes things unreadble
77 var temp2 = (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)) // S0
78 + ((a&hash[1])^(a&hash[2])^(hash[1]&hash[2])); // maj
79
80 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()
81 hash[4] = (hash[4] + temp1)|0;
82 }
83
84 for (i = 0; i < 8; i++) {
85 hash[i] = (hash[i] + oldHash[i])|0;
86 }
87 }
88
89 for (i = 0; i < 8; i++) {
90 for (j = 3; j + 1; j--) {
91 var b = (hash[i]>>(j*8))&255;
92 result += ((b < 16) ? 0 : '') + b.toString(16);
93 }
94 }
95 return result;
96};