Created
November 9, 2012 18:41
-
-
Save ojii/4047433 to your computer and use it in GitHub Desktop.
Base64 for JS (safe for IDs)
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
/* | |
Base64.encode(str): | |
Returns a string that is guaranteed to be a valid ID and can be decoded into its | |
original form. No two inputs generate the same output. | |
Base64.decode(str): | |
Returns the original string from the output of Base64.encode. | |
*/ | |
var Base64 = { | |
characters: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.", | |
'encode': function(data) { | |
if (!data) { | |
return data; | |
} | |
var o1, o2, o3, h1, h2, h3, h4, bits, r, i = 0, ac = 0, enc = '', tmp = []; | |
do { | |
o1 = data.charCodeAt(i++); | |
o2 = data.charCodeAt(i++); | |
o3 = data.charCodeAt(i++); | |
bits = o1 << 16 | o2 << 8 | o3; | |
h1 = bits >> 18 & 0x3f; | |
h2 = bits >> 12 & 0x3f; | |
h3 = bits >> 6 & 0x3f; | |
h4 = bits & 0x3f; | |
tmp[ac++] = Base64.characters.charAt(h1) + Base64.characters.charAt(h2) + Base64.characters.charAt(h3) + Base64.characters.charAt(h4); | |
} while (i < data.length); | |
enc = tmp.join(''); | |
r = data.length % 3; | |
return 'b' + (r ? enc.slice(0, r - 3) : enc) + '...'.slice(r || 3); | |
}, | |
'decode': function(data){ | |
if (!data) { | |
return data; | |
} | |
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, tmp = []; | |
data = data.substr(1); | |
do { | |
h1 = Base64.characters.indexOf(data.charAt(i++)); | |
h2 = Base64.characters.indexOf(data.charAt(i++)); | |
h3 = Base64.characters.indexOf(data.charAt(i++)); | |
h4 = Base64.characters.indexOf(data.charAt(i++)); | |
bits = h1 << 18 | h2 << 12 | h3 << 6 | h4; | |
o1 = bits >> 16 & 0xff; | |
o2 = bits >> 8 & 0xff; | |
o3 = bits & 0xff; | |
if (h3 == 64) { | |
tmp[ac++] = String.fromCharCode(o1); | |
} else if (h4 == 64) { | |
tmp[ac++] = String.fromCharCode(o1, o2); | |
} else { | |
tmp[ac++] = String.fromCharCode(o1, o2, o3); | |
} | |
} while (i < data.length); | |
return tmp.join(''); | |
} | |
}; |
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 g={a:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.",encode:function(a){if(!a)return a;var b,e,c,d,h=0,i=0,f="",f=[];do b=a.charCodeAt(h++),e=a.charCodeAt(h++),c=a.charCodeAt(h++),d=b<<16|e<<8|c,b=d>>18&63,e=d>>12&63,c=d>>6&63,d&=63,f[i++]=g.a.charAt(b)+g.a.charAt(e)+g.a.charAt(c)+g.a.charAt(d);while(h<a.length);f=f.join("");a=a.length%3;return"b"+(a?f.slice(0,a-3):f)+"...".slice(a||3)},decode:function(a){if(!a)return a;var b,e,c,d,h,i=0,f=0,j=[],a=a.substr(1);do b=g.a.indexOf(a.charAt(i++)), | |
e=g.a.indexOf(a.charAt(i++)),d=g.a.indexOf(a.charAt(i++)),h=g.a.indexOf(a.charAt(i++)),c=b<<18|e<<12|d<<6|h,b=c>>16&255,e=c>>8&255,c&=255,64==d?j[f++]=String.fromCharCode(b):64==h?j[f++]=String.fromCharCode(b,e):j[f++]=String.fromCharCode(b,e,c);while(i<a.length);return j.join("")}};window.Base64=g; |
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 encoded = Base64.encode('hello world!!'); | |
console.log(encoded); // "baGVsbG8gd29ybGQhIQ.." | |
var decoded = Base64.decode(encoded); | |
console.log(decoded); // "hello world!!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment