//******************************************************************** // Ver 1.0 // 2020.03.05 1、新建文件。 //******************************************************************** #include "config.h" SHA1_Str SHA1_Info; void SHA1_Init(void) { SHA1_Info.Sha1_h[0]=0x67452301; SHA1_Info.Sha1_h[1]=0xefcdab89; SHA1_Info.Sha1_h[2]=0x98badcfe; SHA1_Info.Sha1_h[3]=0x10325476; SHA1_Info.Sha1_h[4]=0xc3d2e1f0; } void SHA1_Byte2Word(INT8U *ptr,INT16U Len) { INT16U i,j; for(i=0; i= i) && (i >= 0)) { sha1_T+=(SHA1_Char(sha1_B,sha1_C,sha1_D)+SHA1_KT1); } if((39 >= i) && (i >= 20)) { sha1_T+=(SHA1_Parity(sha1_B,sha1_C,sha1_D)+SHA1_KT2); } if((59 >= i) && (i >= 40)) { sha1_T+=(SHA1_Maj(sha1_B,sha1_C,sha1_D)+SHA1_KT3); } if((79 >= i) && (i >= 60)) { sha1_T+=(SHA1_Parity(sha1_B,sha1_C,sha1_D)+SHA1_KT4); } sha1_E=sha1_D; sha1_D=sha1_C; sha1_C=SHA1_SLeft(sha1_B,30); sha1_B=sha1_A; sha1_A=sha1_T; } SHA1_Info.Sha1_h[0]=sha1_A; SHA1_Info.Sha1_h[1]=sha1_B; SHA1_Info.Sha1_h[2]=sha1_C; SHA1_Info.Sha1_h[3]=sha1_D; SHA1_Info.Sha1_h[4]=sha1_E; } //因为是非标版,所以输入必须为8字节的倍数 //本程序中待处理的字符串长度不得大于2^16 bit void SHA1_Process(INT8U *ptr) { INT32U Len[2]; INT8U *ptr_t; INT8U i,j; Len[0]=SHA1_GetStrLen(ptr); //获取数据长度 Len[1]=Len[0]; ptr_t=ptr; SHA1_Init(); while((Len[0]/64) > 0) { SHA1_Byte2Word(ptr_t, 16); SHA1_ProChunk(); SHA1_ClearM(); Len[0]-=64; ptr_t+=64; } SHA1_Byte2Word(ptr_t, Len[0]/4); i=Len[0]%4; j=Len[0]/4; SHA1_Info.Sha1_m[j]=0; switch(Len[0]%4) { case 0: SHA1_Info.Sha1_m[j]|=0x80000000; break; case 1: SHA1_Info.Sha1_m[j]=*ptr_t; SHA1_Info.Sha1_m[j]=SHA1_Info.Sha1_m[j]<<24; SHA1_Info.Sha1_m[j]|=0x00800000; break; case 2: SHA1_Info.Sha1_m[j]=*ptr_t; SHA1_Info.Sha1_m[j]=SHA1_Info.Sha1_m[j]<<8; SHA1_Info.Sha1_m[j]+=*(ptr_t+1); SHA1_Info.Sha1_m[j]=SHA1_Info.Sha1_m[j]<<16; SHA1_Info.Sha1_m[j]|=0x00008000; break; case 3: SHA1_Info.Sha1_m[j]=*ptr_t; SHA1_Info.Sha1_m[j]=SHA1_Info.Sha1_m[j]<<8; SHA1_Info.Sha1_m[j]+=*(ptr_t+1); SHA1_Info.Sha1_m[j]=SHA1_Info.Sha1_m[j]<<8; SHA1_Info.Sha1_m[j]+=*(ptr_t+2); SHA1_Info.Sha1_m[j]=SHA1_Info.Sha1_m[j]<<8; SHA1_Info.Sha1_m[j]|=0x00000080; break; } if((64 > Len[0]) && (Len[0] >=56)) { SHA1_ProChunk(); SHA1_ClearM(); } SHA1_AddLen2Word(Len[1]); SHA1_ProChunk(); }