Skip to content

Instantly share code, notes, and snippets.

@CarbonAlabel
Last active June 14, 2019 09:31
Show Gist options
  • Save CarbonAlabel/434e172a09ccf313f9ede93f88efc653 to your computer and use it in GitHub Desktop.
Save CarbonAlabel/434e172a09ccf313f9ede93f88efc653 to your computer and use it in GitHub Desktop.
Utility functions for implementing a PKCE authorization flow in browsers.
// Utility functions
function base64url_encode(string) {
return btoa(string).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}
function base64url_decode(string) {
return atob(string.replace(/-/g, "+").replace(/_/g, "/"));
}
function random_string(bytes) {
return String.fromCharCode(...crypto.getRandomValues(new Uint8Array(bytes)));
}
async function sha_256(string) {
return String.fromCharCode(...new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(string))));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment