使用说明:
将斜杠下面的源码复制到自己的源码中,在主函数之前加上函数声明,
之后便可在主函数中调用调用该函数了。
参数add是需要散列的数据的地址;
参数Length是需要散列的数据的长度(单位字节);
参数add1是用来接受散列值的内存块的地址,该内存块的长度必须大于等于20字节;
//////////////////////////////////////////////////////////////////////////////////////
void sha1( void *add,unsigned long length,void *add1 )
{
unsigned char tempa;
将斜杠下面的源码复制到自己的源码中,在主函数之前加上函数声明,
之后便可在主函数中调用调用该函数了。
参数add是需要散列的数据的地址;
参数Length是需要散列的数据的长度(单位字节);
参数add1是用来接受散列值的内存块的地址,该内存块的长度必须大于等于20字节;
//////////////////////////////////////////////////////////////////////////////////////
void sha1( void *add,unsigned long length,void *add1 )
{
unsigned char tempa;
unsigned long hash[5];
unsigned long long raw;
unsigned long a,b,c,d,e;
unsigned long buffer[20],temp;
unsigned long offset=0,offset1,offset2;
unsigned long count,count1,count2,count3=7;
hash[0]=a=0x67452301;
hash[1]=b=0xEFCDAB89;
hash[2]=c=0x98BADCFE;
hash[3]=d=0x10325476;
hash[4]=e=0xC3D2E1F0;
raw=length*8;
offset1=length;
offset2=length*8%512;
if(offset2>=0&&offset2<=447)
offset2=(512-offset2)/8;
else
offset2=(1024-offset2)/8;
count=(offset2+length)/64;
offset2=offset2+length-8;
for(count1=1;count1<=count;count1++)
{
for(count2=0;count2<=63;count2++,offset++)
{
if(offset>=0&&offset<offset1)
{
for(count2=0;count2<=63;count2++,offset++)
{
if(offset>=0&&offset<offset1)
{*((unsigned char *)buffer+count2)=*((unsigned char *)add+offset);}
else if (offset==offset1)
{*((unsigned char *)buffer+count2)=128;}
else if (offset>offset1&&offset<offset2)
{*((unsigned char *)buffer+count2)=0;}
else if (offset>=offset2)
{
*((unsigned char *)buffer+count2)=*((unsigned char *)(&raw)+count3);
{
*((unsigned char *)buffer+count2)=*((unsigned char *)(&raw)+count3);
count3--;
}
else
{;}
}
for(count2=0;count2<=15;count2++)
{
tempa=*((unsigned char *)(&buffer[count2])+3);
}
else
{;}
}
for(count2=0;count2<=15;count2++)
{
tempa=*((unsigned char *)(&buffer[count2])+3);
*((unsigned char *)(&buffer[count2])+3)=*((unsigned char *)(&buffer[count2]));
*((unsigned char *)(&buffer[count2]))=tempa;
tempa=*((unsigned char *)(&buffer[count2])+2);
*((unsigned char *)(&buffer[count2])+2)=*((unsigned char *)(&buffer[count2])+1);
*((unsigned char *)(&buffer[count2])+1)=tempa;
}
temp=buffer[13]^buffer[8]^buffer[2]^buffer[0];
}
temp=buffer[13]^buffer[8]^buffer[2]^buffer[0];
buffer[16]=temp<<1|temp>>31;
temp=buffer[14]^buffer[9]^buffer[3]^buffer[1];
buffer[17]=temp<<1|temp>>31;
temp=buffer[15]^buffer[10]^buffer[4]^buffer[2];
buffer[18]=temp<<1|temp>>31;
temp=buffer[16]^buffer[11]^buffer[5]^buffer[3];
buffer[19]=temp<<1|temp>>31;
for(count2=0;count2<=19;count2++)
{
temp=b&c|~b&d;
{
temp=b&c|~b&d;
temp=(a<<5|a>>27)+temp+e+buffer[count2]+0x5A827999;
e=d;d=c;c=b<<30|b>>2;b=a;a=temp;
}
for(count2=0;count2<=19;count2++)
{
temp=buffer[(17+count2)%20]^buffer[(12+count2)%20];
}
for(count2=0;count2<=19;count2++)
{
temp=buffer[(17+count2)%20]^buffer[(12+count2)%20];
temp=temp^buffer[(6+count2)%20]^buffer[(4+count2)%20];
buffer[count2]=temp<<1|temp>>31;
}
for(count2=0;count2<=19;count2++)
{
temp=b^c^d;
}
for(count2=0;count2<=19;count2++)
{
temp=b^c^d;
temp=(a<<5|a>>27)+temp+e+buffer[count2]+0x6ED9EBA1;
e=d;d=c;c=b<<30|b>>2;b=a;a=temp;
}
for(count2=0;count2<=19;count2++)
{
temp=buffer[(17+count2)%20]^buffer[(12+count2)%20];
}
for(count2=0;count2<=19;count2++)
{
temp=buffer[(17+count2)%20]^buffer[(12+count2)%20];
temp=temp^buffer[(6+count2)%20]^buffer[(4+count2)%20];
buffer[count2]=temp<<1|temp>>31;
}
for(count2=0;count2<=19;count2++)
{
temp=b&c|b&d|c&d;
}
for(count2=0;count2<=19;count2++)
{
temp=b&c|b&d|c&d;
temp=(a<<5|a>>27)+temp+e+buffer[count2]+0x8F1BBCDC;
e=d;d=c;c=b<<30|b>>2;b=a;a=temp;
}
for(count2=0;count2<=19;count2++)
{
temp=buffer[(17+count2)%20]^buffer[(12+count2)%20];
}
for(count2=0;count2<=19;count2++)
{
temp=buffer[(17+count2)%20]^buffer[(12+count2)%20];
temp=temp^buffer[(6+count2)%20]^buffer[(4+count2)%20];
buffer[count2]=temp<<1|temp>>31;
}
for(count2=0;count2<=19;count2++)
{
temp=b^c^d;
}
for(count2=0;count2<=19;count2++)
{
temp=b^c^d;
temp=(a<<5|a>>27)+temp+e+buffer[count2]+0xCA62C1D6;
e=d;d=c;c=b<<30|b>>2;b=a;a=temp;
}
hash[0]=hash[0]+a;
hash[1]=hash[1]+b;
hash[2]=hash[2]+c;
hash[3]=hash[3]+d;
hash[4]=hash[4]+e;
a=hash[0];b=hash[1];c=hash[2];d=hash[3];e=hash[4];
}
for(count2=0;count2<=4;count2++)
{
tempa=*((unsigned char *)(&hash[count2])+3);
}
hash[0]=hash[0]+a;
hash[1]=hash[1]+b;
hash[2]=hash[2]+c;
hash[3]=hash[3]+d;
hash[4]=hash[4]+e;
a=hash[0];b=hash[1];c=hash[2];d=hash[3];e=hash[4];
}
for(count2=0;count2<=4;count2++)
{
tempa=*((unsigned char *)(&hash[count2])+3);
*((unsigned char *)(&hash[count2])+3)=*((unsigned char *)(&hash[count2]));
*((unsigned char *)(&hash[count2]))=tempa;
tempa=*((unsigned char *)(&hash[count2])+2);
*((unsigned char *)(&hash[count2])+2)=*((unsigned char *)(&hash[count2])+1);
*((unsigned char *)(&hash[count2])+1)=tempa;
}
*((unsigned long *)add1)=hash[0];
*((unsigned long *)add1+1)=hash[1];
*((unsigned long *)add1+2)=hash[2];
*((unsigned long *)add1+3)=hash[3];
*((unsigned long *)add1+4)=hash[4];
}
*((unsigned long *)add1)=hash[0];
*((unsigned long *)add1+1)=hash[1];
*((unsigned long *)add1+2)=hash[2];
*((unsigned long *)add1+3)=hash[3];
*((unsigned long *)add1+4)=hash[4];
return;
}
}