试图实现SHA-1

时间:2018-02-11 21:21:40

标签: matlab encryption hash cryptography sha

我正在尝试实现SHA-1代码,但是当我尝试计算32位长度的80个字时,我得到了一些奇怪的输出,这里是伪代码。

 message='Hello';
 message=dec2bin(message,8);
  message=reshape(message,1,40);  
message(end:end+1)=0;
a=repmat('0', 1, 409);
message=strcat(message,a);
lengthh=length(dec2bin(40,8));
a=repmat('0', 1, 64-lengthh);
b=dec2bin(40,8);
 message=strcat(message,a,b);
k=1;
s='';
M=repmat(s,16,32);
W= repmat(s,80,32);
for i=1:32:512
M(k,:)=message(i:i+31);
 k=k+1;
end
Hash(1,:)  =dec2bin(hex2dec('67452301'),32);
Hash(2,:) = dec2bin(hex2dec('efcdab89'),32);
Hash(3,:) = dec2bin(hex2dec('98badcfe'),32);
Hash(4,:) = dec2bin(hex2dec('10325476'),32);
Hash(5,:) =dec2bin(hex2dec('c3d2e1f0'),32);

W(1:16,:)=M(1:16,:);
for i=17:80
 W(i,:)=  bitxor(uint32(W(i-3,:)),uint32(W(i-8,:)),'uint32');
 end

输出

val =

   00000111110111100000101110111100
   00101000000000000000000000000000
   00000000000000000000000000000000
   00000000000000000000000000000000
   00000000000000000000000000000000
    00000000000000000000000000000000
   00000000000000000000000000000000
  00000000000000000000000000000000
  00000000000000000000000000000000
    00000000000000000000000000000000
  00000000000000000000000000000000
   00000000000000000000000000000000
  00000000000000000000000000000000
  00000000000000000000000000000000
    00000000000000000000000000000000
   00000000000000000000000000101000



         00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000101000


00000000000000000000000000101000



00000000000000000000000000000000
00000000000000000000000000101000

00000000000000000000000000101000


00000000000000000000000000101000

00000000000000000000000000101000





00000000000000000000000000101000

00000000000000000000000000000000
00000000000000000000000000101000

00000000000000000000000000000000
00000000000000000000000000101000


00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000101000


00000000000000000000000000101000

00000000000000000000000000101000

00000000000000000000000000101000


00000000000000000000000000000000
00000000000000000000000000101000


00000000000000000000000000000000
00000000000000000000000000101000

00000000000000000000000000101000

00000000000000000000000000000000
00000000000000000000000000101000




00000000000000000000000000101000

问题从第17行开始,一些奇怪的符号开始出现,可以有人帮忙。

1 个答案:

答案 0 :(得分:1)

我建议你不要自己实现哈希算法;这可能是一种痛苦,特别是在Matlab中。使用内置的Java哈希框架,这是一种闪电般快速且防弹的方法:

% Your message to be hashed...
message = 'Hello';

% Convert the message into a byte array...
message_bytes = getByteStreamFromArray(double(message));

% Create an instance of a Java MessageDigest with the desired algorithm:
md = java.security.MessageDigest.getInstance('SHA-1');
md.update(message_bytes);

% Properly format the computed hash as an hexadecimal string:
hash = reshape(dec2hex(typecast(md.digest(),'uint8'))',1,[]);

输出为F7FF9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0。例如,您可以使用this online application验证结果的更正效果。