您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 国内外标准规范 > 实验六、SHA-1算法的实现
1上机实验报告(六)实验课程:应用密码学实验时间:2013年11月26日任课教师:刘光军班级:11级信息与计算科学专业1班姓名:刘静学号:0202110123一、实验名称:SHA-1算法的实现二、实验目的1、了解SHA-1算法的基本原理2、掌握SHA-1算法的实现方法三、实验内容1、掌握SHA-1算法的原理及过程2、完成字符串数据的SHA-1运算及算法流程四、报告正文(文档,数据,模型,程序,图形)1、讨论学习SHA-1算法的基本原理,请给出算法的流程描述并画出算法的实现流程图。2、用C或C++设计实现相应的功能模块,并给出最后的测试结果。2#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*,unsignedchar3Message_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;4if(!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)...{5context-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]^6W[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++)...{7temp=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;}8/**//**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_Low
本文标题:实验六、SHA-1算法的实现
链接地址:https://www.777doc.com/doc-5481018 .html