Created
October 15, 2018 01:38
-
-
Save themikefuller/c1de46cbbdad02645b9dc006baedf88e to your computer and use it in GitHub Desktop.
Encode and decode byte arrays to and from base64url encoding in JavaScript.
This file contains hidden or 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
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); | |
})); | |
} |
fiiled to execute atob on window
Interesting. I wrote this code 6 years ago, and I use it pretty regularly in a few libraries. The error you got looks like what happens when the b64urlstring value is not encoded to Base64URL. Maybe the value is straight up Base64? This is for URL-safe encoding. If you can provide me with the string or content you tried to encode/decode, I can see what is causing the error.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fiiled to execute atob on window