|
| 1 | +function generate_code_challenge(verifier) { |
| 2 | + return base64_urlencode(sha256bin(verifier)); |
| 3 | +} |
| 4 | + |
| 5 | +generateCodeChallenge = async (codeVerifier) => { |
| 6 | + const strBuffer = new TextEncoder('utf-8').encode(codeVerifier); |
| 7 | + const hashBuffer = await window.crypto.subtle.digest('SHA-256', strBuffer); |
| 8 | + return Array.from(new Uint8Array(hashBuffer)); |
| 9 | +} |
| 10 | + |
| 11 | +function sha256bin(ascii) { |
| 12 | + return hex2bin(sha256(ascii)); |
| 13 | +} |
| 14 | + |
| 15 | +function hex2bin(s) { |
| 16 | + |
| 17 | + var ret = [] |
| 18 | + var i = 0 |
| 19 | + var l |
| 20 | + |
| 21 | + s += '' |
| 22 | + |
| 23 | + for (l = s.length; i < l; i += 2) { |
| 24 | + var c = parseInt(s.substr(i, 1), 16) |
| 25 | + var k = parseInt(s.substr(i + 1, 1), 16) |
| 26 | + if (isNaN(c) || isNaN(k)) return false |
| 27 | + ret.push((c << 4) | k) |
| 28 | + } |
| 29 | + |
| 30 | + return String.fromCharCode.apply(String, ret) |
| 31 | +} |
| 32 | + |
| 33 | +var sha256 = function sha256(ascii) { |
| 34 | + function rightRotate(value, amount) { |
| 35 | + return (value>>>amount) | (value<<(32 - amount)); |
| 36 | + }; |
| 37 | + |
| 38 | + var mathPow = Math.pow; |
| 39 | + var maxWord = mathPow(2, 32); |
| 40 | + var lengthProperty = 'length'; |
| 41 | + var i, j; // Used as a counter across the whole file |
| 42 | + var result = ''; |
| 43 | + |
| 44 | + var words = []; |
| 45 | + var asciiBitLength = ascii[lengthProperty]*8; |
| 46 | + |
| 47 | + //* caching results is optional - remove/add slash from front of this line to toggle |
| 48 | + // Initial hash value: first 32 bits of the fractional parts of the square roots of the first 8 primes |
| 49 | + // (we actually calculate the first 64, but extra values are just ignored) |
| 50 | + var hash = sha256.h = sha256.h || []; |
| 51 | + // Round constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes |
| 52 | + var k = sha256.k = sha256.k || []; |
| 53 | + var primeCounter = k[lengthProperty]; |
| 54 | + /*/ |
| 55 | + var hash = [], k = []; |
| 56 | + var primeCounter = 0; |
| 57 | + //*/ |
| 58 | + |
| 59 | + var isComposite = {}; |
| 60 | + for (var candidate = 2; primeCounter < 64; candidate++) { |
| 61 | + if (!isComposite[candidate]) { |
| 62 | + for (i = 0; i < 313; i += candidate) { |
| 63 | + isComposite[i] = candidate; |
| 64 | + } |
| 65 | + hash[primeCounter] = (mathPow(candidate, .5)*maxWord)|0; |
| 66 | + k[primeCounter++] = (mathPow(candidate, 1/3)*maxWord)|0; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + ascii += '\x80'; // Append '1' bit (plus zero padding) |
| 71 | + while (ascii[lengthProperty]%64 - 56) ascii += '\x00'; // More zero padding |
| 72 | + for (i = 0; i < ascii[lengthProperty]; i++) { |
| 73 | + j = ascii.charCodeAt(i); |
| 74 | + if (j>>8) return; // ASCII check: only accept characters in range 0-255 |
| 75 | + words[i>>2] |= j << ((3 - i)%4)*8; |
| 76 | + } |
| 77 | + words[words[lengthProperty]] = ((asciiBitLength/maxWord)|0); |
| 78 | + words[words[lengthProperty]] = (asciiBitLength) |
| 79 | + |
| 80 | + // process each chunk |
| 81 | + for (j = 0; j < words[lengthProperty];) { |
| 82 | + var w = words.slice(j, j += 16); // The message is expanded into 64 words as part of the iteration |
| 83 | + var oldHash = hash; |
| 84 | + // This is now the "working hash", often labelled as variables a...g |
| 85 | + // (we have to truncate as well, otherwise extra entries at the end accumulate |
| 86 | + hash = hash.slice(0, 8); |
| 87 | + |
| 88 | + for (i = 0; i < 64; i++) { |
| 89 | + var i2 = i + j; |
| 90 | + // Expand the message into 64 words |
| 91 | + // Used below if |
| 92 | + var w15 = w[i - 15], w2 = w[i - 2]; |
| 93 | + |
| 94 | + // Iterate |
| 95 | + var a = hash[0], e = hash[4]; |
| 96 | + var temp1 = hash[7] |
| 97 | + + (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)) // S1 |
| 98 | + + ((e&hash[5])^((~e)&hash[6])) // ch |
| 99 | + + k[i] |
| 100 | + // Expand the message schedule if needed |
| 101 | + + (w[i] = (i < 16) ? w[i] : ( |
| 102 | + w[i - 16] |
| 103 | + + (rightRotate(w15, 7) ^ rightRotate(w15, 18) ^ (w15>>>3)) // s0 |
| 104 | + + w[i - 7] |
| 105 | + + (rightRotate(w2, 17) ^ rightRotate(w2, 19) ^ (w2>>>10)) // s1 |
| 106 | + )|0 |
| 107 | + ); |
| 108 | + // This is only used once, so *could* be moved below, but it only saves 4 bytes and makes things unreadble |
| 109 | + var temp2 = (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)) // S0 |
| 110 | + + ((a&hash[1])^(a&hash[2])^(hash[1]&hash[2])); // maj |
| 111 | + |
| 112 | + 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() |
| 113 | + hash[4] = (hash[4] + temp1)|0; |
| 114 | + } |
| 115 | + |
| 116 | + for (i = 0; i < 8; i++) { |
| 117 | + hash[i] = (hash[i] + oldHash[i])|0; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + for (i = 0; i < 8; i++) { |
| 122 | + for (j = 3; j + 1; j--) { |
| 123 | + var b = (hash[i]>>(j*8))&255; |
| 124 | + result += ((b < 16) ? 0 : '') + b.toString(16); |
| 125 | + } |
| 126 | + } |
| 127 | + return result; |
| 128 | +}; |
0 commit comments