Skip to content

Instantly share code, notes, and snippets.

@michaelphipps
Created March 18, 2023 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelphipps/f24d619f6594f5f0e3e4ba0df286ecdb to your computer and use it in GitHub Desktop.
Save michaelphipps/f24d619f6594f5f0e3e4ba0df286ecdb to your computer and use it in GitHub Desktop.
Base64url encoding and decoding functions
const base64url = {
encode: (data) => {
let base64 = btoa(String.fromCharCode(...data));
return base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
},
decode: (base64url) => {
let base64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
while (base64.length % 4) base64 += '=';
return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
}
};
@michaelphipps
Copy link
Author

Because it's not like you can't find this in 1 million places already...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment