JSFiddle - React, Tailwind, and code Playground
by Csaba Hellinger
JavaScript
function encode(input, encoding) {
if (encoding === void 0) { encoding = "utf8"; }
if (Buffer.isBuffer(input)) {
return fromBase64(input.toString("base64"));
}
return fromBase64(Buffer.from(input, encoding).toString("base64"));
}
;
function decode(base64url, encoding) {
if (encoding === void 0) { encoding = "utf8"; }
return Buffer.from(toBase64(base64url), "base64").toString(encoding);
}
function toBase64(base64url) {
base64url = base64url.toString();
return pad_string_1.default(base64url)
.replace(/\-/g, "+")
.replace(/_/g, "/");
}
function fromBase64(base64) {
return base64
.replace(/=/g, "")
.replace(/\+/g, "-")
.replace(/\//g, "_");
}
function toBuffer(base64url) {
return Buffer.from(toBase64(base64url), "base64");
}
var base64url = encode;
const testRandomArr = Array.from('hello-world-test-random-string');
const generateCodeVerifier_old = () => {
// algorithm: https://auth0.com/docs/api-auth/tutorials/authorization-code-grant-pkce#1-create-a-code-verifier
const crypto = window && (window.crypto || window.msCrypto);
const randomArr = testRandomArr; // Array.from(crypto.getRandomValues(new Uint8Array(32)));
const randomStr = randomArr.map(String.fromCharCode).join();
const base64 = btoa(randomStr);
const verifier = base64
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
return verifier;
};
const generateCodeVerifier_new = () => {
// algorithm: https://auth0.com/docs/api-auth/tutorials/authorization-code-grant-pkce#1-create-a-code-verifier
const crypto = window && (window.crypto || window.msCrypto);
const randomArr = testRandomArr; // Array.from(crypto.getRandomValues(new Uint8Array(32)));
const randomStr = randomArr.map(String.fromCharCode).join();
const result = base64url(randomStr);
return result;
};
console.log('old', generateCodeVerifier_old());
console.log('new', generateCodeVerifier_new());