here I have shown how to generate base64 alpahbets and encode string in one line...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #generate Base64 Alphabet in one line
b64list = reduce(lambda v,a:v+[chr(ord(a[0])+i) for i in range(a[1])],[('A',26),('a',26),('0',10)],[])+['+','/','=']
#now convert each 24 bit field into 4 pieces
s ='anuraguniyal.'
##bytes = [ord(ch) for ch in s]
##print b64list[bytes[0]>>2]
##print b64list[((bytes[0]&3)<<4)+(bytes[1]>>4)]
##print b64list[((bytes[1]&15)<<2)+(bytes[2]>>6)]
##print b64list[bytes[2]&63]
#this can be written in short as
#one line to convert three bytes to 4 base64 chars
en = lambda a,b,c,l = b64list:''.join([l[a>>2]]+[l[i or 64] for i in [((a&3)<<4)+(b>>4),((b&15)<<2)+(c>>6),c&63]])
#use en function to convert aribirary legth strings
l = [ord(c) for c in s]+[0,0,0]
ll = []
for i in range(0,len(s),3):
ll.extend(en(l[i],l[i+1],l[i+2]))
#this can be written in one ilne
encode = lambda s,l=[[]]:''.join([l.pop(0),l.append([ord(c) for c in s]+[0,0,0]),[en(l[0][i],l[0][i+1],l[0][i+2]) for i in range(0,len(s),3)]][2])
#combining en and encode
encode = lambda s,l=[[]]:''.join([l.pop(0),l.append([ord(c) for c in s]+[0,0,0]),[apply(lambda a,b,c,l = b64list:''.join([l[a>>2]]+[l[i or 64] for i in [((a&3)<<4)+(b>>4),((b&15)<<2)+(c>>6),c&63]]),(l[0][i],l[0][i+1],l[0][i+2])) for i in range(0,len(s),3)]][2])
#base64 Alphabet is not integrated in this oneliner as it will evaluate it each time
#that may slow this function
#but just to write one liner base 64 converter
#here it is...10 times slower
encode = lambda s,l=[[]]:''.join([l.pop(0),l.append([ord(c) for c in s]+[0,0,0]),[apply(lambda a,b,c,l = reduce(lambda v,a:v+[chr(ord(a[0])+i) for i in range(a[1])],[('A',26),('a',26),('0',10)],[])+['+','/','=']:''.join([l[a>>2]]+[l[i or 64] for i in [((a&3)<<4)+(b>>4),((b&15)<<2)+(c>>6),c&63]]),(l[0][i],l[0][i+1],l[0][i+2])) for i in range(0,len(s),3)]][2])
print '-----------------------'
import base64
print encode(s)
print base64.encodestring(s)
|
U can use it if for some mysterious reason you don't want to use base64 module.
Tags: shortcuts
Argh, thanks for making your Python code look like Perl.
LOL. 1 line.
Since this is the top google search for the corresponding perl one-liner - here it is:-
use bytes; while(<DATA>){tr#A-Za-z0-9+/##cd;tr#A-Za-z0-9+/# -_#; $l= pack("c",32+0.75*length($_));print unpack("u",$l.$_);}
that takes incoming base64 and outputs the binary version. It works by converting the base64 into the perl uuencode format first. (it also removes all non-base64-chars in anyone is being pedantic.
You can create some base64 to feed it with like so:-
perl -e 'use MIME::Base64; open(IN,"<file.gz");binmode(in);sysread(in,$x,9999999); print="" encode_base64($x)'<="" p="">
Chris.
Yeah... another broken anti-XSS web page comment form. Sigh. Replace everything above from the "print" part with this, assuming this web page doesn't break it as well:-
print encode_base64($x)' >tst64