SHA1算法C语言实现

本文详细介绍了如何实现SHA-1散列算法,包括参数解释、内部逻辑和关键步骤,帮助开发者理解并应用这一安全散列算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用说明:
  将斜杠下面的源码复制到自己的源码中,在主函数之前加上函数声明,
  之后便可在主函数中调用调用该函数了。
  参数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)
           {*((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);
            count3--;
         }
         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];
      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=(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];
        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;
        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];
        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;
        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];
        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;
        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);
    *((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];
  return;
  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值