SHA1算法源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

SHA1算法源代码2009-10-1617:51sha1.h====================================================================================#ifndef_SHA1_H_#define_SHA1_H_/**//*sha1.h*/#ifndef_SHA_enum_#define_SHA_enum_enum...{shaSuccess=0,shaNull,/**//*Nullpointerparameter*/shaInputTooLong,/**//*inputdatatoolong*/shaStateError/**//*calledInputafterResult*/};#endif#defineSHA1HashSize20typedefstructSHA1Context...{DWORDIntermediate_Hash[SHA1HashSize/4];//MessageDigestDWORDLength_Low;//MessagelengthinbitsDWORDLength_High;//MessagelengthinbitsintMessage_Block_Index;//IndexintomessageblockarrayunsignedcharMessage_Block[64];//512-bitmessageblocksintComputed;//Isthedigestcomputed?intCorrupted;//Isthemessagedigestcorrupted?}SHA1Context;//FunctionPrototypesCStringGetSHA1String(CStringsSource);intSHA1Reset(SHA1Context*);intSHA1Input(SHA1Context*,constunsignedchar*,unsignedint);intSHA1Result(SHA1Context*,unsignedcharMessage_Digest[SHA1HashSize]);CStringSha1toBase32(constunsignedchar*);#endif==================================================================================#includesha1.h//DefinetheSHA1circularleftshiftmacro#defineSHA1CircularShift(bits,word)(((word)(bits))|((word)(32-(bits))))//LocalFunctionPrototyptes*/voidSHA1PadMessage(SHA1Context*);voidSHA1ProcessMessageBlock(SHA1Context*);intSHA1Reset(SHA1Context*c)...{if(!c)returnshaNull;c-Length_Low=0;c-Length_High=0;c-Message_Block_Index=0;c-Intermediate_Hash[0]=0x67452301;c-Intermediate_Hash[1]=0xEFCDAB89;c-Intermediate_Hash[2]=0x98BADCFE;c-Intermediate_Hash[3]=0x10325476;c-Intermediate_Hash[4]=0xC3D2E1F0;c-Computed=0;c-Corrupted=0;returnshaSuccess;}intSHA1Result(SHA1Context*c,unsignedcharMessage_Digest[SHA1HashSize])...{inti;if(!c||!Message_Digest)returnshaNull;if(c-Corrupted)returnc-Corrupted;if(!c-Computed)...{SHA1PadMessage(c);for(i=0;i64;c-Message_Block[++i]=0);c-Length_Low=0;/**//*andclearlength*/c-Length_High=0;c-Computed=1;}for(i=0;iSHA1HashSize;++i)Message_Digest[i]=c-Intermediate_Hash[i2]8*(3-(i&0x03));returnshaSuccess;}intSHA1Input(SHA1Context*context,constunsignedchar*message_array,unsignedlength)...{if(!length)returnshaSuccess;if(!context||!message_array)returnshaNull;if(context-Computed)...{context-Corrupted=shaStateError;returnshaStateError;}if(context-Corrupted)returncontext-Corrupted;while(length--&&!context-Corrupted)...{context-Message_Block[context-Message_Block_Index++]=(*message_array&0xFF);context-Length_Low+=8;if(context-Length_Low==0)...{context-Length_High++;if(context-Length_High==0)...{/**//*Messageistoolong*/context-Corrupted=1;}}if(context-Message_Block_Index==64)SHA1ProcessMessageBlock(context);message_array++;}returnshaSuccess;}voidSHA1ProcessMessageBlock(SHA1Context*context)...{constDWORDK[]=...{0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6};intt;DWORDtemp;DWORDW[80];DWORDA,B,C,D,E;/**//**Initializethefirst16wordsinthearrayW*/for(t=0;t16;t++)...{W[t]=context-Message_Block[t*4]24;W[t]|=context-Message_Block[t*4+1]16;W[t]|=context-Message_Block[t*4+2]8;W[t]|=context-Message_Block[t*4+3];}for(t=16;t80;t++)W[t]=SHA1CircularShift(1,W[t-3]^W[t-8]^W[t-14]^W[t-16]);A=context-Intermediate_Hash[0];B=context-Intermediate_Hash[1];C=context-Intermediate_Hash[2];D=context-Intermediate_Hash[3];E=context-Intermediate_Hash[4];for(t=0;t20;t++)...{temp=SHA1CircularShift(5,A)+((B&C)|((~B)&D))+E+W[t]+K[0];E=D;D=C;C=SHA1CircularShift(30,B);B=A;A=temp;}for(t=20;t40;t++)...{temp=SHA1CircularShift(5,A)+(B^C^D)+E+W[t]+K[1];E=D;D=C;C=SHA1CircularShift(30,B);B=A;A=temp;}for(t=40;t60;t++)...{temp=SHA1CircularShift(5,A)+((B&C)|(B&D)|(C&D))+E+W[t]+K[2];E=D;D=C;C=SHA1CircularShift(30,B);B=A;A=temp;}for(t=60;t80;t++)...{temp=SHA1CircularShift(5,A)+(B^C^D)+E+W[t]+K[3];E=D;D=C;C=SHA1CircularShift(30,B);B=A;A=temp;}context-Intermediate_Hash[0]+=A;context-Intermediate_Hash[1]+=B;context-Intermediate_Hash[2]+=C;context-Intermediate_Hash[3]+=D;context-Intermediate_Hash[4]+=E;context-Message_Block_Index=0;}voidSHA1PadMessage(SHA1Context*context)...{if(context-Message_Block_Index55)...{context-Message_Block[context-Message_Block_Index++]=0x80;while(context-Message_Block_Index64)context-Message_Block[context-Message_Block_Index++]=0;SHA1ProcessMessageBlock(context);while(context-Message_Block_Index56)context-Message_Block[context-Message_Block_Index++]=0;}else...{context-Message_Block[context-Message_Block_Index++]=0x80;while(context-Message_Block_Index56)context-Message_Block[context-Message_Block_Index++]=0;}/**//**Storethemessagelengthasthelast8octets*/context-Message_Block[56]=context-Length_High24;context-Message_Block[57]=context-Length_High16;context-Message_Block[58]=context-Length_High8;context-Message_Block[59]=context-Length_High;context-Message_Block[60]=context-Length_Low24;context-Message_Block[61]=context-Length_Low16;context-Message_Block[62]=context-Length_Low8;context-Message_Block[63]=context-Length_Low;SHA1ProcessMessageBlock(context);}//Convert5Bytesto8BytesBase32vo

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功