Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
base64 encoder w/padding

140byt.es - Base64 encoder

A base64encoder with padding, what shall i say more? This version now includes the amazing improvements made by Jonas Magazinius and LeverOne, for more details visit the "sla.ckers"-forum @ http://sla.ckers.org/forum/read.php?24,36342

If you are looking for a decoder give https://gist.github.com/atk/1020396 a try.

Example

	var 
		sigma ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
	,	sample = "140bytes rocks!"
	,	encoded = (function(...){...})(sample, sigma);
function(
a, /* input text */
b, /* mapping table */
c, /* working block */
d, /* input char index */
e /* output text */ ){
for(
// initialize char indices
d=e='';
// cast d to int (floor)
// if the next input index does not exist:
// change the mapping table to "="
// check if d has no fractional digits
a[d|0]||(b='=',d%1);
e+=b[ 63 & c >> 8 - d % 1 * 8 ] // "8 - d % 1 * 8" generates the sequence 2, 4, 6, 8 (first value for d is 0.75)
)c = c << 8 | a.charCodeAt( d-=-.75 ); // note: "d -= -3/4" works too
return e
}
function(a,b,c,d,e){for(d=e='';a[d|0]||(b='=',d%1);e+=b[63&c>>8-d%1*8])c=c<<8|a.charCodeAt(d-=-.75);return e}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "Base64Encoder",
"description": "A JavaScript Base64 encoder in 109 Bytes.",
"keywords": ["base64", "encode", "rfc2045", "sla.ckers", "btoa"],
"license" : "DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE",
"url" : "http://sla.ckers.org/forum/read.php?24,36342"
}
<html>
<body>
<script type="text/javascript">
(function(){
var f = function(a,b,c,d,e){for(d=e='';a[d|0]||(b='=',d%1);e+=b[63&c>>8-d%1*8])c=c<<8|a.charCodeAt(d-=-.75);return e}
, test = {
'' : ''
,'AA==' : '\0'
,'AAA=' : '\0\0'
,'AAAA' : '\0\0\0'
,'AAEC' : '\0\1\2'
,"Zg==" : "f"
,"Zm8=" : "fo"
,"Zm9v" : "foo"
,"Zm9vYg==" : "foob"
,"Zm9vYmE=" : "fooba"
,"Zm9vYmFy" : "foobar"
}
, error = 0;
;
for( i in test ) {
var r = f(test[i], "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", "=");
if( r != i ) {
error++;
document.writeln( 'Expected &quot;'+i+'&quot; for &quot;'+test[i]+'&quot; but got &quot;'+ r + "&quot;<br>" );
}
}
if( error > 0) {
document.writeln( "<br>"+error+ " tests failed!<br>" );
} else {
document.writeln( "<br>Everything is fine!<br>" );
}
})();
</script>
</body>
</html>
@Kambfhase

This comment has been minimized.

Show comment Hide comment
@Kambfhase

Kambfhase May 31, 2011

ris not declared and spills the global scope. Since you do have enough chars left that should be no problem though.

ris not declared and spills the global scope. Since you do have enough chars left that should be no problem though.

@jed

This comment has been minimized.

Show comment Hide comment
@jed

jed May 31, 2011

this is still blowing my mind.

jed commented May 31, 2011

this is still blowing my mind.

@atk

This comment has been minimized.

Show comment Hide comment
@atk

atk Jun 11, 2011

This will not work in IE7 and older versions. Since only few people are using this old browser replacements, I would not consider this critical.

atk commented Jun 11, 2011

This will not work in IE7 and older versions. Since only few people are using this old browser replacements, I would not consider this critical.

@jed

This comment has been minimized.

Show comment Hide comment
@jed

jed Jul 11, 2011

hey @nignag, would you mind taking the comments out of your package.json?

jed commented Jul 11, 2011

hey @nignag, would you mind taking the comments out of your package.json?

@Jarod-

This comment has been minimized.

Show comment Hide comment
@Jarod-

Jarod- Jul 27, 2011

Any idea what I should change to make it work for IE7 and older ? In my company everyone is using IE<=9 BUT with IE7 compatibility mode (blocked by I/T), and this code doesn't work in that case :-/
Thanks.

Jarod- commented Jul 27, 2011

Any idea what I should change to make it work for IE7 and older ? In my company everyone is using IE<=9 BUT with IE7 compatibility mode (blocked by I/T), and this code doesn't work in that case :-/
Thanks.

@Kambfhase

This comment has been minimized.

Show comment Hide comment
@Kambfhase

Kambfhase Jul 27, 2011

try chaning the bracket notation on line 16 and 18 to .charAt(). otherwise sneak up to your co-workers stations and install chromeframe. :)

try chaning the bracket notation on line 16 and 18 to .charAt(). otherwise sneak up to your co-workers stations and install chromeframe. :)

@Jarod-

This comment has been minimized.

Show comment Hide comment
@Jarod-

Jarod- Jul 27, 2011

@Kambfhase yes it works like that. Thanks!

Jarod- commented Jul 27, 2011

@Kambfhase yes it works like that. Thanks!

@jed

This comment has been minimized.

Show comment Hide comment
@jed

jed Jul 27, 2011

wait, did .charAt work or did sneaking up on your coworkers work? nyuk.

jed commented Jul 27, 2011

wait, did .charAt work or did sneaking up on your coworkers work? nyuk.

@atk

This comment has been minimized.

Show comment Hide comment
@atk

atk Jul 27, 2011

you could also use .split('') on both input string and translation map.

atk commented Jul 27, 2011

you could also use .split('') on both input string and translation map.

@LeverOne

This comment has been minimized.

Show comment Hide comment
@LeverOne

LeverOne Feb 3, 2012

109 now if you had not seen: http://sla.ckers.org/forum/read.php?24,36342,page=2#msg-42491
Here are the tests if anyone is interested: http://jsperf.com/base64-encode-js

LeverOne commented Feb 3, 2012

109 now if you had not seen: http://sla.ckers.org/forum/read.php?24,36342,page=2#msg-42491
Here are the tests if anyone is interested: http://jsperf.com/base64-encode-js

@atk

This comment has been minimized.

Show comment Hide comment
@atk

atk Feb 6, 2012

@LeverOne: (d-=-.75) looks like it can be reduced to (d+=.75)

atk commented Feb 6, 2012

@LeverOne: (d-=-.75) looks like it can be reduced to (d+=.75)

@LeverOne

This comment has been minimized.

Show comment Hide comment
@LeverOne

LeverOne Feb 6, 2012

@atk
Unfortunately, not. This is a trick to prevent the concatenation, since d is a string initially. Therefore, d+=.75 == '.75', then '.75.75', etc.

LeverOne commented Feb 6, 2012

@atk
Unfortunately, not. This is a trick to prevent the concatenation, since d is a string initially. Therefore, d+=.75 == '.75', then '.75.75', etc.

@atk

This comment has been minimized.

Show comment Hide comment
@atk

atk Feb 6, 2012

@LeverOne: thanks for clarification.

atk commented Feb 6, 2012

@LeverOne: thanks for clarification.

@tsaniel

This comment has been minimized.

Show comment Hide comment
@tsaniel

tsaniel Feb 18, 2012

@LeverOne: I always wonder how you find so many magic sequences. Could you tell us how?

tsaniel commented Feb 18, 2012

@LeverOne: I always wonder how you find so many magic sequences. Could you tell us how?

@LeverOne

This comment has been minimized.

Show comment Hide comment
@LeverOne

LeverOne Feb 19, 2012

@tsaniel
This process is almost creative, there are no special spells.

@tsaniel
This process is almost creative, there are no special spells.

@atkach

This comment has been minimized.

Show comment Hide comment
@atkach

atkach Dec 23, 2013

Could you please explain me what this "d%1" in "a[d|0]||(b='=',d%1);". Why don't just "a[d|0]||b='=';"?

atkach commented Dec 23, 2013

Could you please explain me what this "d%1" in "a[d|0]||(b='=',d%1);". Why don't just "a[d|0]||b='=';"?

@nignag

This comment has been minimized.

Show comment Hide comment
@nignag

nignag Apr 27, 2014

d%1 tests for fractional digits, so the check is necessary

Owner

nignag commented Apr 27, 2014

d%1 tests for fractional digits, so the check is necessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment