I guess I technically have to claim my own bounty for ripemd160
. Sorry about the formatting, but it should paste back into Mathematica nicely.
(*
This function finds the RIPEMD160 hash of a list of bytes expressed as integers.
Integers greater than 255 and less than 0 are truncated to 8 bits.
This code was adapted from: http://www.win.tue.nl/~berry/2WC01/CryptographicHash-Collisions.nb
*)
ripemd160[bytelist_List/;Or[And@@IntegerQ/@bytelist, Length[bytelist]==0]]:=Module[{f,y,z,s,h1,h2,h3,h4,h5,a,b,c,d,e,t,m,X,r,xx,i,j,k, x, BitRotateLeft, convert},
x = Flatten[IntegerDigits[bytelist, 2, 8]];
BitRotateLeft[x_,n_] := FromDigits[ RotateLeft[IntegerDigits[x,2,32],n],2];
convert[x_] := FromDigits[Reverse[IntegerDigits[x,256,4]],256];
f[j_,u_,v_,w_]:=
If[j<=16, BitXor[u,v,w],
If[j<=32, BitOr[BitAnd[u,v],BitAnd[BitNot[u],w]],
If[j<=48, BitXor[BitOr[u,BitNot[v]],w],
If[j<=64, BitOr[BitAnd[u,w],BitAnd[v,BitNot[w]]],
BitXor[u,BitOr[v,BitNot[w]]]
]]]];
y[0,j_] := y[0,j] =If[j<=16, 0, If[j<=32, 16^^5a827999, If[j<=48, 16^^6ed9eba1, If[j<=64,16^^8f1bbcdc,16^^a953fd4e]] ]];
y[1,j_] := y[1,j] =If[j<=16, 16^^50a28be6, If[j<=32, 16^^5c4dd124, If[j<=48, 16^^6d703ef3, If[j<=64,16^^7a6d76e9,0]] ]];
z[0]=Flatten[{{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},{7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8},
{3,10,14,4,9,15,8,1,2,7,0,6,13,11,5,12},{1,9,11,10,0,8,12,4,13,3,7,15,14,5,6,2},{4,0,5,9,7,12,2,10,14,1,3,8,11,6,15,13}}];
z[1]=Flatten[{{5,14,7,0,9,2,11,4,13,6,15,8,1,10,3,12},{6,11,3,7,0,13,5,10,14,15,8,12,4,9,1,2},{15,5,1,3,7,14,6,9,11,8,12,2,10,0,4,13},{8,6,4,1,3,11,15,0,5,12,2,13,9,7,10,14},{12,15,10,4,1,5,8,7,6,2,13,14,0,3,9,11}}];
s[0]=Flatten[{{11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8},{7,6,8,13,11,9,7,15,7,12,15,9,11,7,13,12},{11,13,6,7,14,9,13,15,14,8,13,6,5,12,7,5},{11,12,14,15,14,15,9,8,9,14,5,6,8,6,5,12},{9,15,5,11,6,8,13,12,5,12,13,14,11,8,5,6}}];
s[1]=Flatten[{{8,9,9,11,13,15,15,5,7,7,8,11,14,14,12,6},{9,13,15,7,12,8,9,11,7,7,12,7,6,15,13,11},{9,7,15,11,8,6,6,14,12,13,5,14,13,13,7,5},{15,5,8,11,14,14,6,14,6,9,12,9,12,5,15,8},{8,5,12,9,12,5,14,6,8,13,6,5,15,13,11,11}}];
m=Ceiling[(Length[x]+65)/512];
r = 512m-Length[x]-64;
xx = Join[x,{1},Table[0,{r-1}],Flatten[IntegerDigits[Reverse[IntegerDigits[Mod[Length[x],2^64],256,8]],2,8]]];
{h1,h2,h3,h4,h5} = {16^^67452301,16^^efcdab89,16^^98badcfe,16^^10325476,16^^c3d2e1f0};
For[i=0, iFor[j=0, j<16, j++, X[j]=FromDigits[Take[xx,512i+32j+{1,32}],2];];
For[k=0,k<=1,k++,
{a[k],b[k],c[k],d[k],e[k]} = {h1,h2,h3,h4,h5};
For[j=1, j<=80, j++, t = Mod[a[k] + f[(1-k) j+k(81-j),b[k],c[k],d[k]] +convert[ X[z[k][[j]]]] + y[k,j],2^32];
{a[k],b[k],c[k],d[k],e[k]} = {e[k],Mod[e[k]+BitRotateLeft[t,s[k][[j]]],2^32],b[k],BitRotateLeft[c[k],10],d[k]};
];
];
{h1,h2,h3,h4,h5}=Mod[{h2+c[0]+d[1],h3+d[0]+e[1],h4+e[0]+a[1],h5+a[0]+b[1],h1+b[0]+c[1]},2^32];
];
FromDigits[convert/@{h1,h2,h3,h4,h5},2^32]
];
If you want to add ripemd160 to the default Mathematica fuction Hash[], here's how for the most useful pattern types: