Skip to content

Instantly share code, notes, and snippets.

@jackinf
Created December 24, 2018 10:30
Show Gist options
  • Save jackinf/c5b9710efdf14eaf26398349002e4ae5 to your computer and use it in GitHub Desktop.
Save jackinf/c5b9710efdf14eaf26398349002e4ae5 to your computer and use it in GitHub Desktop.
function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment