Skip to content

Instantly share code, notes, and snippets.

@voronoipotato
Forked from geraintluff/base64-web-safe.js
Created January 23, 2018 19: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 voronoipotato/94b798429c270627c409463011b4bda4 to your computer and use it in GitHub Desktop.
Save voronoipotato/94b798429c270627c409463011b4bda4 to your computer and use it in GitHub Desktop.
Convert base64 to and from web-safe variant
// Convert from normal to web-safe, strip trailing "="s
function webSafe64(base64) {
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
// Convert from web-safe to normal, add trailing "="s
function normal64(base64) {
return base64.replace(/\-/g, '+').replace(/_/g, '/') + '=='.substring(0, (3*base64.length)%4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment