No BSD License  

Implementation of md5 Algorithm Occupying Less Memory and takes only 1.35 seconds

by

 

13 Oct 2005 (Updated )

In this algorithm constant datas are created by logic thus occupies less memory.

mainpro.m
fname=input('Input File (in ASCII format)? ','s');
hash_foutname=input('Output File for md5 Hash? ','s');
t1=clock;
%Open the input file and get the first line of data
fid=fopen(fname);
M = fread(fid);
fclose(fid);


ini=dec2bin(M(1),8);

for ii = 2:length(M)
    ini=cat(2,ini,dec2bin(M(ii),8));
end

s2=length(ini)/8;
 block_temp = [ ini, ... 
                 '1', ... 
                 num2str(zeros(mod(448-1-s2*8,512),1))' ... 
                dec2bin(s2*8,64) ]; 
            nb=length(block_temp)/512;
block = reshape(block_temp,512,nb)';

for i=1:64
      T(i)=cellstr(dec2hex(floor(2^32*abs(sin(i))),8));
end

   
for b=1:nb
 H = [ '01','23','45','67';...
      '89','ab','cd','ef';...
      'fe','dc','ba','98';...
      '76','54','32','10'];
H1=H;
    
inp=reshape(block(b,:),32,16)';

%disp('round1');
%Round1
shift=7;
for i=1:16
   
      
a=tarr(H(1,:));
b=tarr(H(2,:));      
c=tarr(H(3,:));      
d=tarr(H(4,:)); 
  

    a=d;
    d=c;
    c=b;
    
t1= bin2dec2(f('round1',b,c,d));
t2= bin2dec2(tarr(T(i)));
t3= bin2dec2(inp(i,:));
t4=b;
anst=dec2bin(mod(t1+t2+t3+t4,2^32),32);
ans=cls(anst,shift);
b=dec2bin(mod(bin2dec2(ans)+bin2dec2(b),2^32),32);

if shift==22
    shift=7;
else shift=shift+5;
end
H=updateH(H,a,b,c,d);

end


%Round2
shift=5;shi=0;
for i=17:32
%disp('round2');
a=tarr(H(1,:));
b=tarr(H(2,:));      
c=tarr(H(3,:));      
d=tarr(H(4,:)); 
     

    a=d;
    d=c;
    c=b;
    
t1= bin2dec2(f('round2',b,c,d));
t2= bin2dec2(tarr(T(i)));
itemp=mod((1+5*i),16)+1;
t3= bin2dec2(inp(itemp,:));
t4=b;
anst=dec2bin(mod(t1+t2+t3+t4,2^32),32);
ans=cls(anst,shift);
b=dec2bin(mod(bin2dec2(ans)+bin2dec2(b),2^32),32);

shi=shi+1;
if shift==20
    shift=5;shi=0;
else shift=shift+3+shi;
end
H=updateH(H,a,b,c,d);

end
count=1;

%Round3

for i=33:48
%disp('round3');      
a=tarr(H(1,:));
b=tarr(H(2,:));      
c=tarr(H(3,:));      
d=tarr(H(4,:)); 
     

    a=d;
    d=c;
    c=b;
    
t1= bin2dec2(f('round3',b,c,d));
t2= bin2dec2(tarr(T(i)));
itemp=mod((5+3*i),16)+1;
t3= bin2dec2(inp(itemp,:));
t4=b;
anst=dec2bin(mod(t1+t2+t3+t4,2^32),32);
if count==1
    shift=4;
elseif count==2
        shift=11;
elseif count==3
            shift==16;
elseif count==4
                shift=23;
            end
ans=cls(anst,shift);
b=dec2bin(mod(bin2dec2(ans)+bin2dec2(b),2^32),32);

H=updateH(H,a,b,c,d);

if count==4
    count=0;
end

count=count+1;

        end

%Round4

shift=6;shi=0;
for i=49:64
 %   disp('round4');
a=tarr(H(1,:));
b=tarr(H(2,:));      
c=tarr(H(3,:));      
d=tarr(H(4,:)); 
    

    a=d;
    d=c;
    c=b;
    
t1= bin2dec2(f('round4',b,c,d));
t2= bin2dec2(tarr(T(i)));
itemp=mod(7*i,16)+1;
t3= bin2dec2(inp(itemp,:));
t4=b;
anst=dec2bin(mod(t1+t2+t3+t4,2^32),32);

ans=cls(anst,shift);

shi=shi+1;
if shift==21
    shift=6;shi=0;
else shift=shift+3+shi;
end

b=dec2bin(mod(bin2dec2(ans)+bin2dec2(b),2^32),32);



H=updateH(H,a,b,c,d);


end


H1=calculate_new_md5(H1,a,b,c,d);

end
disp(H1);
t2=clock;
timedur=t2-t1;
etime(clock,timedur)

fid=fopen(hash_foutname,'w+');
fprintf(fid,'%s',H1(1,:));
fprintf(fid,'%s',H1(2,:));
fprintf(fid,'%s',H1(3,:));
fprintf(fid,'%s',H1(4,:));

fclose(fid);



































Contact us