zoom1 zoom2 zoom3

philippewang.info

MD5 SUM

This (very special) program is written in Objective Caml.
It computes the MD5SUM of each file given on the command line, just like the program md5sum.
It has only 31 lines of maximum 80 characters, and is portable (i.e. architecture independant).
BookeldOr wrote one that has only 14 lines, but in C, for 32bit-little-endian architecture only.

SOMME MD5

Ce programme (très spécial) est écrit en Objective Caml.
Il calcule la somme MD5 de chaque fichier passé en argument sur la ligne de commande, tout comme le programme md5sum.
Il ne fait que 31 lignes de maximum 80 caractères, et est portable (i.e. ne dépend pas de l'architecture machine).
BookeldOr en a écrit une version qui ne fait que 14 lignes, mais en C, pour architecture 32 bits petit boutien uniquement.
open String module I=Int32 module J=Int64 let n,v,(<),(=),($),(&),(|/),(&*),(@),
(++),(>*),w,m,l,(%)=I.lognot,Sys.argv,I.shift_left,I.shift_right_logical,I.add,I
.logand,I.logor,J.logand,I.logxor,J.add,J.shift_right_logical,sub,Array.make,
Array.length,List.iter let(><)i x=(i<x)|/(i=(32-x))and r,f=let f x=I.of_int(Char
.code x)in(let c x=Char.chr(I.to_int x)in fun n->let s=create 8in let a,b=J.
to_int32(n>*32),J.to_int32 n in s.[4]<-c(255l&a);s.[5]<-c(a=8&255l);s.[6]<-c(a=
16&255l);s.[7]<-c(a=24&255l);s.[0]<-c(255l&b);s.[1]<-c(b=8&255l);s.[2]<-c(b=16&
255l);s.[3]<-c(b=24&255l);s),(fun s->let a,b,c,d=f s.[0],(f s.[1])<8,(f s.[2])<
16,(f s.[3])<24in a|/b|/c|/d)let u s=let x=m 16 0l in for i=0to 15do x.(i)<-f(w
s(i*4)4)done;x and t=let t=m 64 0l in for i=0to l t-1do t.(i)<-J.to_int32(J.
of_float(ldexp(abs_float(sin(float(i+1))))32))done;t and g x=let l=J.
to_int((J.sub 960L(511L&*x))&*511L)in let l=if not(l<>0)then 512else l in let s=
make((l+1)/8)'\000'in s.[0]<-'\128';s^(r x)let rec i()=j:=(!j+1)mod 64;!j and j=
ref(-1)and h xb a b c d=j:=-1;let aa,bb,cc,dd= !a,!b,!c,!d in let rec x()=p:=(!p
+1)mod 4;match!p with 0->a,b,c,d|3->b,c,d,a|2->c,d,a,b|_->d,a,b,c and p=ref 0and
o()=p:= -1in(o();let k=ref(-1)in let m()=incr k;!k in let f s=let(a,b,c,d),k=x()
,m()in a:= !b$(!a$((!b& !c)|/((n!b)& !d))$xb.(k)$t.(i())><s)in f%[7;12;17;22;7;
12;17;22;7;12;17;22;7;12;17;22];o();let f(k,s)=let(a,b,c,d)=x()in a:= !b$(!a$((!
b& !d)|/(!c&(n !d)))$xb.(k)$t.(i())><s)in f%[1,5;6,9;11,14;0,20;5,5;10,9;15,14;4
,20;9,5;14,9;3,14;8,20;13,5;2,9;7,14;12,20];o();let f(k,s)=let(a,b,c,d)=x()in a
:= !b$(!a$(!b@ !c@ !d)$xb.(k)$t.(i())><s)in f%[5,4;8,11;11,16;14,23;1,4;4,11;7,
16;10,23;13,4;0,11;3,16;6,23;9,4;12,11;15,16;2,23];o();let f(k,s)=let(a,b,c,d)=x
()in a:= !b$(!a$(!c@(!b|/(n !d)))$xb.(k)$t.(i())><s)in f%[0,6;7,10;14,15;5,21;12
,6;3,10;10,15;1,21;8,6;15,10;6,15;13,21;4,6;11,10;2,15;9,21];a:= !a$aa;b:= !b$bb
;c:= !c$cc;d:= !d$dd)let p l f=(fun a->(Printf.printf"%02lx")%[a<24=24;a=8<24=24
;a=16<24=24;a=24<24=24])%l;Printf.printf" %s\n"f let z f=let a,b,c,d=ref (* * *)
0x67452301l,ref 0xefcdab89l,ref 0x98badcfel,ref 0x10325476l in let ic,bf=open_in
f,create 64in let rec e ea=match input ic bf 0 64with 0->h(u(g ea))a b c d|64->h
(u bf)a b c d;e(ea++512L)|l->let ea=ea++(J.of_int(8*l))in let s=(w bf 0 l)^(g ea
)in let sl=length s in if sl>64 then(h(u(w s 0 64))a b c d;h(u(w s 64(sl-64)))a
b c d)else h(u s)a b c d in e 0L;p[!a;!b;!c;!d]f;;for i=1to l v-1do z v.(i)done

It is possible to reduce this code ! I did it, but the modifications are minor. When I have time to waste, I'll check the concepts in this program to reduce it again... (the most compact version has 29 lines)
Maybe it's hard to believe... so here it is.

Il est possible de réduire encore ce code. Je l'ai fait, mais les modifications sont mineures, et peuvent presque être considérées comme syntaxiques. Quand j'aurai du temps à perdre, je réduirai à nouveau ce code en révisant les concepts utilisés... (la version la plus compacte fait actuellement 29 lignes)
C'est peut-être difficile à croire... Alors le voici.


:: philippewang.info ::

:: design & photos by Philippe Wang :: XHTML 1.1 :: CSS 2 :: RSS 2 :: stats :: contact ::
:: Best viewed with Safari or Opera or Firefox or Links :: No SPAM Please ::
 
This page was generated on Sun Nov 18 16:58:31 CET 2007 by BashGXD