Created
March 18, 2023 08:26
-
-
Save michaelphipps/f24d619f6594f5f0e3e4ba0df286ecdb to your computer and use it in GitHub Desktop.
Base64url encoding and decoding functions
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
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)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because it's not like you can't find this in 1 million places already...