Skip to content

Instantly share code, notes, and snippets.

@bseib
Created July 29, 2015 15:45
Show Gist options
  • Save bseib/35796c59303d45d6b98e to your computer and use it in GitHub Desktop.
Save bseib/35796c59303d45d6b98e to your computer and use it in GitHub Desktop.
/**
* This code snippet came from an answer I posted on stackoverflow and was derived from work presented
* among other answers and comments. The post is here:
* http://stackoverflow.com/questions/2820249/base64-encoding-and-decoding-in-client-side-javascript/15016605#15016605
*
* This is not very readable code, and would be better served by being written out such that it is understandable, and
* letting a minifier tool do the minification. However, it does work and is easy to copy and paste where needed rather
* than creating a larger library dependency when that is not desirable. So take it at face value.
*
* I posted it here when someone requested to use it and asked about a license. You are free to use this code however
* you wish -- copy it, modify it, distribute it. I make no claims on it, and I do not ask for attribution. I also do not
* warrant anything, so use this code at your own risk.
*
*/
/**
* This function presumes a well formed base64 string 's' with no carriage returns.
*/
decodeBase64 = function(s) {
var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for(i=0;i<64;i++){e[A.charAt(i)]=i;}
for(x=0;x<L;x++){
c=e[s.charAt(x)];b=(b<<6)+c;l+=6;
while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
}
return r;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment