Last active
August 31, 2015 12:30
-
-
Save rexso/a3541c05096ba9174915 to your computer and use it in GitHub Desktop.
JavaScript Base64 encoder/decoder (minified)
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
var base64={table:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",decode:function(t,e,r){function a(t){if(-1==(t=d.indexOf(t.charAt(0))))throw"invalid characater detected in base64.decode";return t}if(!(t=""+t).length)throw"invalid string length in base64.decode";var h,n,i=[],c=t.length,s=0,d=e?this.table.substr(0,62)+"-_":this.table;if(0==c||c%4&&!r)throw"invalid base64 string detected in base64.decode";for(c%4&&r?(s=4-c%4,c-=4):"="==t.charAt(c-1)&&(s="="==t.charAt(c-2)?2:1,c-=4),n=0;c>n;)h=a(t[n++])<<18|a(t[n++])<<12|a(t[n++])<<6|a(t[n++]),i.push(String.fromCharCode(h>>16,h>>8&255,255&h));switch(s){case 1:h=a(t[n++])<<18|a(t[n++])<<12|a(t[n++])<<6,i.push(String.fromCharCode(h>>16,h>>8&255));break;case 2:h=a(t[n++])<<18|a(t[n++])<<12,i.push(String.fromCharCode(h>>16))}return i.join("")},encode:function(t,e,r){function a(t){if((t=t.charCodeAt(0))>255)throw"invalid character detected in base64.encode";return t}if(!(t=""+t).length)throw"invalid string length in base64.encode";var h,n,i=[],c=t.length-t.length%3,s=e?this.table.substr(0,62)+"-_":this.table;for(n=0;c>n;)h=a(t[n++])<<16|a(t[n++])<<8|a(t[n++]),i.push(s.charAt(h>>18)),i.push(s.charAt(h>>12&63)),i.push(s.charAt(h>>6&63)),i.push(s.charAt(63&h));switch(t.length-c){case 1:h=a(t[n])<<16,i.push(s.charAt(h>>18)+s.charAt(h>>12&63)+(r?"":"=="));break;case 2:h=a(t[n])<<16|a(t[++n])<<8,i.push(s.charAt(h>>18)+s.charAt(h>>12&63)+s.charAt(h>>6&63)+(r?"":"="))}return i.join("")}}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment