Last active
June 14, 2019 09:31
-
-
Save CarbonAlabel/434e172a09ccf313f9ede93f88efc653 to your computer and use it in GitHub Desktop.
Utility functions for implementing a PKCE authorization flow in browsers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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