Skip to content

Instantly share code, notes, and snippets.

@Marcotsept
Forked from geraintluff/base64-web-safe.js
Created September 28, 2018 03:40
Show Gist options
  • Save Marcotsept/a71858b755c77701e85295de103be274 to your computer and use it in GitHub Desktop.
Save Marcotsept/a71858b755c77701e85295de103be274 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