安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准 (Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。当接收到消息的时候,这个消息摘要可以用来验证数据的完整性。在传输的过程中,数据很可能会发生变化,那么这时候就会产生不同的消息摘要。 SHA1有如下特性:不可以从消息摘要中复原信息;两个不同的消息不会产生同样的消息摘要。
算法实现的版本比较多,以下代码来自:http://download.youkuaiyun.com/detail/zhangrulzu/2936159,代码行数很少,但确实实现了想要的效果。
下载的SHA-1算法:
#include<stdio.h>
void creat_w(unsigned char input[64],unsigned long w[80])
{
int i,j;unsigned long temp,temp1;
for(i=0;i<16;i++)
{
j=4*i;
w[i]=((long)input[j])<<24 |((long)input[1+j])<<16|((long)input[2+j])<<8|((long)input[3+j])<<0;
}
for(i=16;i<80;i++)
{
w[i]=w[i-16]^w[i-14]^w[i-8]^w[i-3];
temp=w[i]<<1;
temp1=w[i]>>31;
w[i]=temp|temp1;
}
}
char ms_len(long a,char intput[64])
{
unsigned long temp3,p1; int i,j;
temp3=0;
p1=~(~temp3<<8);
for(i=0;i<4;i++)
{
j=8*i;
intput[63-i]=(char)((a&(p1<<j))>>j);
}
}
main()
{
unsigned long H0=0x67452301,H1=0xefcdab89,H2=0x98badcfe,H3=0x10325476,H4=0xc3d2e1f0;
unsigned long A,B,C,D,E,temp,temp1,temp2,temp3,k,f;int i,flag;unsigned long w[80];
unsigned char input[64]; long x;int n;
printf("input message:\n");
scanf("%s",input);
n=strlen(input);
if(n<57)
{
x=n*8;
ms_len(x,input);
if(n==56)
for(i=n;i<60;i++)
input[i]=0;
else

本文介绍了如何使用C语言实现SHA-1算法,并详细记录了在Visual Studio 2005中编译遇到的错误及其解决过程,包括添加头文件、类型转换等。最终通过调试验证了算法的正确性。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



