Cenzic 232 Patent
Paid Advertising
sla.ckers.org is
ha.ckers sla.cking
Sla.ckers.org
Script obfuscation, filter evasion, IDS/IPS/WAF bypassing... this is where it should live. Because this topic is too big to live anywhere else. Phj33r! 
Go to Topic: PreviousNext
Go to: Forum ListMessage ListNew TopicSearchLog In
Pages: 12Next
Current Page: 1 of 2
140bytes challenges
Posted by: Gareth Heyes
Date: May 25, 2011 06:56AM

https://gist.github.com/962807

So this is pretty awesome there is a challenge to get certain stuff in js within 140 tweetable code. The first challenge is base64. They have an entry but it doesn't work correctly, it doesn't work on IE and doesn't encode single chars correctly.

I had a shot and so far got 147:
function(a,b,c,d,e,f){d=f='';for(e in a)d+='0'+a[e].charCodeAt().toString(2);(d+'0000').replace(/.{6}/g,function($){f+=b[parseInt($,2)]});return f}

This should work for single characters such as "a" and multiple chars but doesn't support padding :( so slackers c'mon can you get it to 140 and correctly encode with padding?

Oh I forgot you call it like this:-
x('a'.split(''),'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')

with whatever name you give the func

Rules:
1. Works on IE9, Chrome, Safari, Firefox
2. <= 140
3. Encodes correctly including single chars
4. Bonus points for padding

------------------------------------------------------------------------------------------------------------
"People who say it cannot be done should not interrupt those who are doing it.";
labs : [www.businessinfo.co.uk]
blog : [www.thespanner.co.uk]
Hackvertor : [hackvertor.co.uk]



Edited 2 time(s). Last edit at 05/25/2011 06:59AM by Gareth Heyes.

Options: ReplyQuote
Re: 140bytes challenges
Date: May 25, 2011 07:23AM

I'm working on it, but in the mean time I think you're function is wrong on some inputs:
x('\xff\xff\xff'.split(''),'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')

----------------34----------------
_=/.+?('['_='+_(_)]+).+/,'_='+_(_)

Options: ReplyQuote
Re: 140bytes challenges
Posted by: Gareth Heyes
Date: May 25, 2011 07:59AM

Yeah it ain't perfect :) I'm sure you and lever_one can blow it away!

The reason it's broken with \xFF is that the result doesn't require zero padding but to save chars I did it on everything

------------------------------------------------------------------------------------------------------------
"People who say it cannot be done should not interrupt those who are doing it.";
labs : [www.businessinfo.co.uk]
blog : [www.thespanner.co.uk]
Hackvertor : [hackvertor.co.uk]



Edited 1 time(s). Last edit at 05/25/2011 08:16AM by Gareth Heyes.

Options: ReplyQuote
Re: 140bytes challenges
Date: May 25, 2011 09:35AM

Some major WTFs when reading this implementation of base64:
http://www.herongyang.com/encoding/Base64-Goetz-JavaScript-Implementation.html

How about:

if (uc != true && uc != false) return null;


LOL!

----------------34----------------
_=/.+?('['_='+_(_)]+).+/,'_='+_(_)

Options: ReplyQuote
Re: 140bytes challenges
Posted by: Gareth Heyes
Date: May 25, 2011 10:43AM

True or not to be true null is the question LOL

------------------------------------------------------------------------------------------------------------
"People who say it cannot be done should not interrupt those who are doing it.";
labs : [www.businessinfo.co.uk]
blog : [www.thespanner.co.uk]
Hackvertor : [hackvertor.co.uk]

Options: ReplyQuote
Re: 140bytes challenges
Date: May 25, 2011 12:31PM

Ok, here's one. Not very optimized, but handles all input with correct padding:

y='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
x=function(a)a.charCodeAt();
'\xff\xff\xff'.replace(/(.)(.)?(.)?/g,function(_,a,b,c)y[x(a)>>2]+y[63&x(a)<<4|x(b)>>4]+(b?y[63&x(b)<<2|x(c)>>6]:'=')+(c?y[63&x(c)]:'='))

----------------34----------------
_=/.+?('['_='+_(_)]+).+/,'_='+_(_)



Edited 1 time(s). Last edit at 05/25/2011 12:48PM by Jonas Magazinius.

Options: ReplyQuote
Re: 140bytes challenges
Posted by: Gareth Heyes
Date: May 25, 2011 12:42PM

Here's a FF one for fun:

('0'+[''.charCodeAt.call(x,i).toString(2)for(i in x='abc')].join(0)).replace(/.{1,6}/g,function(c)b[parseInt(c,2)])

------------------------------------------------------------------------------------------------------------
"People who say it cannot be done should not interrupt those who are doing it.";
labs : [www.businessinfo.co.uk]
blog : [www.thespanner.co.uk]
Hackvertor : [hackvertor.co.uk]

Options: ReplyQuote
Re: 140bytes challenges
Posted by: LeverOne
Date: May 28, 2011 01:27AM

RULES: [github.com]
Quote

Encode: a function that takes the string input and map string ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/, return the base64 encoding. Bonus points for proper "=" padding. [UPDATE] Much progress has been made here, but it's still not cross-browser due to lack of executable RegExps in IE.

b64encode=

function(a,b){for(var c=d=e=f='';c=0,a[+f];){for(d=3;d--;c|=a.charCodeAt(f++)<<d*8);for(d=4;d--;)e+=a[f-d-!!e]?b[c>>d*6&63]:'='}return e}  
// 137

/* or
function(a,b,c,d,e,f){for(e=f='';c=0,a[+f];){for(d=3;d--;c|=a.charCodeAt(f++)<<d*8);for(d=4;d--;)e+=a[f-d-!!e]?b[c>>d*6&63]:'='}return e}  
// 137
*/

alert(b64encode('a\r\nab','ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'))

1. Correct padding for 8- and 16-bits input.
2. Any chars within 0-255 (dec).
3. Leakage in global scope is eliminated (using "var" etc.)
4. IE 8-9, Chrome, Safari, Firefox, Opera

LeverOne

----------------------
~Veritas~

Options: ReplyQuote
Re: 140bytes challenges
Posted by: Wisec
Date: May 30, 2011 01:04PM

we <3 LeverOne :)

--
All Your Flash Are Belong To Us
http://www.wisec.it Blog
http://www.mindedsecurity.com Company

Options: ReplyQuote
Re: 140bytes challenges
Posted by: LeverOne
Date: May 30, 2011 01:23PM

@Wisec

PoC? :)

-------------
^
| At first I took it as а "3 bytes shorter version". LOL
-------------

UPDATE 06/11/2001

Here's the history of base64 encoder outside sla.ckers

1. [gist.github.com] by bytespider

158, '=' - in map with index 64
Quote

function(a,b,c,d,e,f,g,h){h="";for(d=0;a[d];h+=b[e>>2]+b[e<<4&63|f>>4]+b[f<<2&63|g>>6||64]+b[g&63||64])for(c=4;c<7;)arguments[c++]=a.charCodeAt(d++);return h}

2. [gist.github.com] by aemkei

140, w/o padding, !IE
Quote

function(a,b,c,d,e,f,g){for(g=c=e="";(d=a.charCodeAt(g))&&(c+=/.{8}$/(1e7+d.toString(2))),f=c.substr(g++*6,6);)e+=b[parseInt(f,2)];return e}

3. [gist.github.com] by nignag <-- possibly noma

A lot of bright ideas in revisions; padding as an independent variable in the function declaration.

----------------------
~Veritas~



Edited 4 time(s). Last edit at 03/10/2012 03:31PM by LeverOne.

Options: ReplyQuote
Re: 140bytes challenges
Date: May 30, 2011 02:43PM

@LeverOne: Brilliant work as usual! But I managed to reduce it a bit..

129 chars:
function(a,b){for(var c=d=e=f='';a[+f];){for(;c=c<<8|a.charCodeAt(f++),f%3;);for(d=4;d--;)e+=b[a[f-d-!!e]?c>>d*6&63:64]}return e}

----------------34----------------
_=/.+?('['_='+_(_)]+).+/,'_='+_(_)

Options: ReplyQuote
Re: 140bytes challenges
Posted by: LeverOne
Date: May 30, 2011 02:56PM

Quote

But I managed to reduce it a bit

Only when you change the rules. :)

Quote

and map string ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

How much do you have if you do not change the rules?

upd: I guess that 130. Nice!

----------------------
~Veritas~



Edited 1 time(s). Last edit at 05/30/2011 03:09PM by LeverOne.

Options: ReplyQuote
Re: 140bytes challenges
Date: May 31, 2011 05:26PM

Ok, I think I've reached a fixpoint for now.

No cheating, no leaking, merged two loops.

123 chars:
function(a,b,c,d,e,f){for(e=f='';b[1];d=4)for(c=c<<8|a.charCodeAt(f++);!(f%3)&&d--;b=a[f-d]?b:'=')e+=b[c>>d*6&63];return e}

----------------34----------------
_=/.+?('['_='+_(_)]+).+/,'_='+_(_)

Options: ReplyQuote
Re: 140bytes challenges
Posted by: Gareth Heyes
Date: May 31, 2011 06:19PM

:O

Amazing!

------------------------------------------------------------------------------------------------------------
"People who say it cannot be done should not interrupt those who are doing it.";
labs : [www.businessinfo.co.uk]
blog : [www.thespanner.co.uk]
Hackvertor : [hackvertor.co.uk]

Options: ReplyQuote
Re: 140bytes challenges
Posted by: LeverOne
Date: May 31, 2011 06:49PM

121:
function(a,b,c,d,e,f){for(e=f='';b[1];d=4)for(c=c<<8|a.charCodeAt(f++);f%3?0:d--;b=a[f-d]?b:'=')e+=b[c>>d*6&63];return e}

----------------------
~Veritas~

Options: ReplyQuote
Re: 140bytes challenges
Posted by: LeverOne
Date: May 31, 2011 07:11PM

120:

function(a,b,c,d,e,f){for(e=f='';b[d=4];)for(c=c<<8|a.charCodeAt(f++);f%3?0:d--;b=a[f-d]?b:'=')e+=b[c>>d*6&63];return e}

----------------------
~Veritas~

Options: ReplyQuote
Re: 140bytes challenges
Posted by: LeverOne
Date: May 31, 2011 10:05PM

Previous versions of the code (123,121,120) contains an error: if the input string is empty, they return "A===". Fixed version again 123 bytes.


function(a,b,c,d,e,f){for(e=f='';a&&b[d=4];)for(c=c<<8|a.charCodeAt(f++);f%3?0:d--;b=a[f-d]?b:'=')e+=b[c>>d*6&63];return e}

----------------------
~Veritas~



Edited 2 time(s). Last edit at 05/31/2011 10:08PM by LeverOne.

Options: ReplyQuote
Re: 140bytes challenges
Posted by: Skyphire
Date: June 01, 2011 05:29AM

Another easy way of getting the binary bit pattern:

s = 'hello',k = '';

for(i in s) k += '0'+s.charCodeAt(i).toString(2);

Then simply do a modulo 6 in the base64 char index.

Options: ReplyQuote
Re: 140bytes challenges
Date: June 01, 2011 12:17PM

This is still work in progress, but I have to share this.

Single loop, incorrect padding and can't handle null chars, but it's beautiful!

99 chars:
function(a,b,c,d,e,f){for(e=f='';!d--||f%3?c=c<<8|a.charCodeAt(f++,d=4):e+=b[c>>d*6&63];);return e}


Single loop, correct padding, but can't handle null chars:

115 chars:
function(a,b,c,d,e,f){for(e=f='';!d--||f%3?c=c<<8|a.charCodeAt(f++,d=4):e+=a[f-d-!!e]?b[c>>d*6&63]:'=';);return e}


Now fellow sla.ckers, I think this can be corrected and reduced even further. Let's do this together!

----------------34----------------
_=/.+?('['_='+_(_)]+).+/,'_='+_(_)

Options: ReplyQuote
Re: 140bytes challenges
Posted by: Gareth Heyes
Date: June 01, 2011 12:36PM

How about making it without the lookup table?

Something like:
c=(c<27?c+65:c<53?c+70:c<57?c-4:47);

Which could obviously be shorter but you get the idea

------------------------------------------------------------------------------------------------------------
"People who say it cannot be done should not interrupt those who are doing it.";
labs : [www.businessinfo.co.uk]
blog : [www.thespanner.co.uk]
Hackvertor : [hackvertor.co.uk]

Options: ReplyQuote
Re: 140bytes challenges
Posted by: LeverOne
Date: June 02, 2011 04:20PM

115 chars version has the following issues:

1. 'abc' --> 'YWJjA===' (extra iteration if the input string is divisible by 3)
2. '\x00abc' --> '' (null character caused c = 0 in the first iteration)
3. 'abc\0\0\0\x00abc' --> 'YWJjAAAA' (same problem via 4 iteration)

This is variants to fix only for 1.

// 125
function(a,b,c,d,e,f){e=f='';for(;!d--|f%3?c=a[f]||~d?c<<8|a.charCodeAt(f++,d=4):0:e+=a[f-d-!!e]?b[c>>d*6&63]:'=';);return e}
// 122
function(a,b,c,d,e,f){e=f='';for(;!d--|f%3?c=c<<(~d?8:16)|a.charCodeAt(f++,d=4):e+=a[f-d-!!e]?b[c>>d*6&63]:'=';);return e}
function(a,b,c,d,e,f){e=f='';for(;!d--|f%3?c=c<<8|a.charCodeAt(f++,d=4):e+=a[f-d-!!e]?b[c>>(d?d:c=0)*6&63]:'=';);return e}
// 121
function(a,b,c,d,e,f){e=f='';for(;!d--|f%3?c=c<<8|a.charCodeAt(f++,d=4):e+=a[f-d-!!e]?b[c>>d*6&63]:'=';d?0:c=0);return e}

As we see it less than we need to shorten 123.

Quote

How about making it without the lookup table?

In my opinion is more important to add support for 2-bytes characters.
Or do base64decode (which is slightly more complicated due to the fact that the two methods are necessary "search" + "String.fromCharCode")

----------------------
~Veritas~

Options: ReplyQuote
Re: 140bytes challenges
Posted by: noma
Date: June 02, 2011 04:39PM

120:

function(a,b,c,d,e,f){e=f='';for(;!d|f%3?c=c<<8|a.charCodeAt(f++,d=4):e+=a[!e+f-d--]?b[c>>d*6&63]:'=';d?0:c=0);return e}

119:

function(a,b,c,d,e,f){for(e=f='';!d|f%3?c=c<<8|a.charCodeAt(f++,d=4):e+=a[!e+f-d--]?b[c>>d*6&63]:'=';c=d?c:0);return e}



Edited 2 time(s). Last edit at 06/02/2011 05:01PM by noma.

Options: ReplyQuote
Re: 140bytes challenges
Posted by: Gareth Heyes
Date: June 02, 2011 07:03PM

@noma

Welcome to the forums, assuming that code works you're awesome! Nice work

------------------------------------------------------------------------------------------------------------
"People who say it cannot be done should not interrupt those who are doing it.";
labs : [www.businessinfo.co.uk]
blog : [www.thespanner.co.uk]
Hackvertor : [hackvertor.co.uk]

Options: ReplyQuote
Re: 140bytes challenges
Posted by: LeverOne
Date: June 02, 2011 07:14PM

118:

function(a,b,c,d,e,f){for(e=f='';!d|f%3?c=(d?c:0)<<8|a.charCodeAt(f++,d=4):e+=a[!e+f-d--]?b[c>>d*6&63]:'=';);return e}


@noma

Welcome :)

----------------------
~Veritas~

Options: ReplyQuote
Re: 140bytes challenges
Posted by: LeverOne
Date: June 02, 2011 07:38PM

116:

function(a,b,c,d,e,f){for(e=f='';!d|f%3?c=d*c/d<<8|a.charCodeAt(f++,d=4):e+=a[!e+f-d--]?b[c>>d*6&63]:'=';);return e}

aaand!

----------------------
~Veritas~

Options: ReplyQuote
Re: 140bytes challenges
Posted by: noma
Date: June 02, 2011 09:30PM

115 (again):

function(a,b,c,e,i,j){for(e=i='';!j|i%3?c=c*j<<6|a.charCodeAt(i++,j=4):e+=a[!e+i-j--]?b[c>>j*6&63]:'=';);return e}

thx for the little welcome :)

It will be interesting to see when no further byte can be squeezed out


---

Hmmmm, none of the single loop versions can handle 0-input strings like "\0", "\0\0", "\0\0\0", ....



Edited 1 time(s). Last edit at 06/03/2011 07:46AM by noma.

Options: ReplyQuote
Re: 140bytes challenges
Posted by: LeverOne
Date: June 03, 2011 04:54PM

@noma

1. nice move!
2. you have 114.
3. and yes, it was fix for issue №1 only. Full fix for single loop version looks like this:

function(a,b,c,e,i,j){for(e=i='';!j|i%3?!!a[+i]|i%3|(c=j*c<<6|a.charCodeAt(i++,j=4)):e+=a[!e+i-j--]?b[c>>j*6&63]:'=';);return e}

upd: 126

function(a,b,c,e,i,j){for(e=i='';!j|i%3?j|(c=j*c<<6|a.charCodeAt(i++,j=4))||a[i-1]:e+=a[!e+i-j--]?b[c>>j*6&63]:'=';);return e}


upd: 120

function(a,b,c,e,i,j){for(e=i='';a[+i]||j;)!j|i%3?c=c<<8|a.charCodeAt(i++,j=4):e+=a[!e+i-j--]?b[c>>j*6&63]:'=';return e}

----------------------
~Veritas~



Edited 5 time(s). Last edit at 06/03/2011 11:54PM by LeverOne.

Options: ReplyQuote
Re: 140bytes challenges
Posted by: noma
Date: June 04, 2011 03:37PM

117:

function(c,d,b,a,g,e){for(b=a=e="";c[+b]||a;d=c[b++]?d:"=")e+=d[63&(g=g<<8|c.charCodeAt(b-=!(a=a+2&7)))>>a];return e}

Options: ReplyQuote
Re: 140bytes challenges
Posted by: jedschmidt
Date: June 09, 2011 07:58AM

going, going...

(the work in this thread has been inspiring, btw.)

Options: ReplyQuote
Re: 140bytes challenges
Date: June 09, 2011 10:13PM

Back from my short vacation!

First off, fantastic work noma! I was trying to do something similar before, but never got it quite right. Your innovation inspired me to cut it down even further:

113 chars:
function(a,b,c,d,e,f){for(d=e=f='';a[d++]||(b='=')&&e;e&=3)f+=b[63&(c=c<<8|a.charCodeAt(d-=!e++))>>e*2];return f}

Actually 112 chars:
function(a,b,c,d,e,f){for(d=f='';e&=3,a[d++]||(b='=')&&e;)f+=b[63&(c=c<<8|a.charCodeAt(d-=!e))>>++e*2];return f}

----------------34----------------
_=/.+?('['_='+_(_)]+).+/,'_='+_(_)



Edited 1 time(s). Last edit at 06/10/2011 09:05AM by Jonas Magazinius.

Options: ReplyQuote
Pages: 12Next
Current Page: 1 of 2


Sorry, only registered users may post in this forum.