function output_str = MD5(orign_str)
% orign_str 待hash的原始字符串
% output_str md5算法输出的结果
% 设置标准向量,注意内存的大小端存储
a = '67452301';
b = 'efcdab89';
c = '98badcfe';
d = '10325476';
% 将标准向量转化为二进制串表示,
for i = 1:8
a_dec(i) = hex2dec(a(i));
a_bin(i,:) = dec2bin(a_dec(i),4);
b_dec(i) = hex2dec(b(i));
b_bin(i,:) = dec2bin(b_dec(i),4);
c_dec(i) = hex2dec(c(i));
c_bin(i,:) = dec2bin(c_dec(i),4);
d_dec(i) = hex2dec(d(i));
d_bin(i,:) = dec2bin(d_dec(i),4);
end
% 将原来8x4的矩阵转换为1x32的矩阵,方便后续运算
A = reshape(a_bin',1,32);
B = reshape(b_bin',1,32);
C = reshape(c_bin',1,32);
D = reshape(d_bin',1,32);
% 将转换好的向量作为初始向量进行初始化
AA = A;
BB = B;
CC = C;
DD = D;
% 将输入的字符串转换为对应的ascii码值矩阵
str_len = length(orign_str);
input_ascii = abs(orign_str);
% 确定分组个数,512位为一组,一组为64字节,最后需要64位(8字节)存储原字符串的长度
group = ceil((str_len+8)/64); % 对分组个数向上取整,不足一组的按一组记
% 对原始数据进行填充
for k = 1:group*64
if k<=str_len
% 将字符串码值放到前面
input_ascii_1(k) = input_ascii(k);
elseif k==str_len+1
% 字符串结束之后,从二进制上看需要在字符串结束之后第一位用1,之后用0进行填充
% 形如1000 0000 转换为十进制就是128,
input_ascii_1(k) = 128;
elseif k==group*64-7
% 倒数第8个字节存放二进制长度模256的余数
input_ascii_1(k) = mod(str_len*8,256);
elseif k==group*64-6
% 倒数第7个字节存放二进制长度被256除的商,利用商和余数可以还原出原始长度。
input_ascii_1(k) = str_len*8/256;
else
% 其余位置用0填充
input_ascii_1(k) = 0;
end
end
% 对每个分组依次进行计算
for g = 1:group
k = 1;
% 调整数据的顺序与结构
for i = 1:16
for j = 4:-1:1
input_change(i,j) = input_ascii_1(64*(g-1)+k);
k = k + 1;
end
end
input = reshape(dec2bin(input_change',8)',32,16)';
% disp(input);
% 利用正弦值生成后续操作中所需的常量
for i = 1:64
T(i,:) = dec2bin(floor(2^32*abs(sin(i))),32);
end
M = input;
% 对输入作4轮共64次计算
A = FF(A,B,C,D,M,7,T,1,1);
D = FF(D,A,B,C,M,12,T,2,2);
C = FF(C,D,A,B,M,17,T,3,3);
B = FF(B,C,D,A,M,22,T,4,4);
A = FF(A,B,C,D,M,7,T,5,5);
D = FF(D,A,B,C,M,12,T,6,6);
C = FF(C,D,A,B,M,17,T,7,7);
B = FF(B,C,D,A,M,22,T,8,8);
A = FF(A,B,C,D,M,7,T,9,9);
D = FF(D,A,B,C,M,12,T,10,10);
C = FF(C,D,A,B,M,17,T,11,11);
B = FF(B,C,D,A,M,22,T,12,12);
A = FF(A,B,C,D,M,7,T,13,13);
D = FF(D,A,B,C,M,12,T,14,14);
C = FF(C,D,A,B,M,17,T,15,15);
B = FF(B,C,D,A,M,22,T,16,16);
A = GG(A,B,C,D,M,5,T,2,17);
D = GG(D,A,B,C,M,9,T,7,18);
C = GG(C,D,A,B,M,14,T,12,19);
B = GG(B,C,D,A,M,20,T,1,20);
A = GG(A,B,C,D,M,5,T,6,21);
D = GG(D,A,B,C,M,9,T,11,22);
C = GG(C,D,A,B,M,14,T,16,23);
B = GG(B,C,D,A,M,20,T,5,24);
A = GG(A,B,C,D,M,5,T,10,25);
D = GG(D,A,B,C,M,9,T,15,26);
C = GG(C,D,A,B,M,14,T,4,27);
B = GG(B,C,D,A,M,20,T,9,28);
A = GG(A,B,C,D,M,5,T,14,29);
D = GG(D,A,B,C,M,9,T,3,30);
C = GG(C,D,A,B,M,14,T,8,31);
B = GG(B,C,D,A,M,20,T,13,32);
A = HH(A,B,C,D,M,4,T,6,33);
D = HH(D,A,B,C,M,11,T,9,34);
C = HH(C,D,A,B,M,16,T,12,35);
B = HH(B,C,D,A,M,23,T,15,36);
A = HH(A,B,C,D,M,4,T,2,37);
D = HH(D,A,B,C,M,11,T,5,38);
C = HH(C,D,A,B,M,16,T,8,39);
B = HH(B,C,D,A,M,23,T,11,40);
A = HH(A,B,C,D,M,4,T,14,41);
D = HH(D,A,B,C,M,11,T,1,42);
C = HH(C,D,A,B,M,16,T,4,43);
B = HH(B,C,D,A,M,23,T,7,44);
A = HH(A,B,C,D,M,4,T,10,45);
D = HH(D,A,B,C,M,11,T,13,46);
C = HH(C,D,A,B,M,16,T,16,47);
B = HH(B,C,D,A,M,23,T,3,48);
A = II(A,B,C,D,M,6,T,1,49);
D = II(D,A,B,C,M,10,T,8,50);
C = II(C,D,A,B,M,15,T,15,51);
B = II(B,C,D,A,M,21,T,6,52);
A = II(A,B,C,D,M,6,T,13,53);
D = II(D,A,B,C,M,10,T,4,54);
C = II(C,D,A,B,M,15,T,11,55);
B = II(B,C,D,A,M,21,T,2,56);
A = II(A,B,C,D,M,6,T,9,57);
D = II(D,A,B,C,M,10,T,16,58);
C = II(C,D,A,B,M,15,T,7,59);
B = II(B,C,D,A,M,21,T,14,60);
A = II(A,B,C,D,M,6,T,5,61);
D = II(D,A,B,C,M,10,T,12,62);
C = II(C,D,A,B,M,15,T,3,63);
B = II(B,C,D,A,M,21,T,10,64);
% 将四轮运算之后的结果与初始的标准向量做模加运算
A_dec = bin2dec(A);
B_dec = bin2dec(B);
C_dec = bin2dec(C);
D_dec = bin2dec(D);
AA_dec = bin2dec(AA);
BB_dec = bin2dec(BB);
CC_dec = bin2dec(CC);
DD_dec = bin2dec(DD);
A_dec = mod((A_dec + AA_dec),2^32);
B_dec = mod((B_dec + BB_dec),2^32);
C_dec = mod((C_dec + CC_dec),2^32);
D_dec = mod((D_dec + DD_dec),2^32);
if g==group
% 如果是最后一组就将四组值做级联,并调整合适的形式进行输出
output_str = dec2hex([A_dec;B_dec;C_dec;D_dec],8);
% 考虑内存大小端存储的不同
output_str = output_str(:,[7 8 5 6 3 4 1 2])';
output_str = output_str(:)';
else
% 如果不是最后一组,就将上一组的结果作为下一组的初始值带入运算
A = dec2bin(A_dec,32);
B = dec2bin(B_dec,32);
C = dec2bin(C_dec,32);
D = dec2bin(D_dec,32);
AA = A;
BB = B;
CC = C;
DD = D;
end
end
end
- 1
- 2
- 3
- 4
前往页