Skip to content

Instantly share code, notes, and snippets.

@themikefuller
Created October 15, 2018 01:38
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 themikefuller/c1de46cbbdad02645b9dc006baedf88e to your computer and use it in GitHub Desktop.
Save themikefuller/c1de46cbbdad02645b9dc006baedf88e to your computer and use it in GitHub Desktop.
Encode and decode byte arrays to and from base64url encoding in JavaScript.
function base64EncodeURL(byteArray) {
return btoa(Array.from(new Uint8Array(byteArray)).map(val => {
return String.fromCharCode(val);
}).join('')).replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
}
function base64DecodeURL(b64urlstring) {
return new Uint8Array(atob(b64urlstring.replace(/-/g, '+').replace(/_/g, '/')).split('').map(val => {
return val.charCodeAt(0);
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment