资源描述:
《sha哈希算法(delphi源码)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、SHA哈希算法源码unitSHAUnit;interfacetypePSHACtx=^TSHACtx;TSHACtx=recordstate:array[0..7]ofLongWord;length,curlen:Int64;buf:array[0..63]ofByte;end;procedureSHAInit(varmd:TSHACtx);procedureSHAUpdate(varmd:TSHACtx;buf:Pointer;len:Int64;sz:Word);functionSHAFina
2、l(varmd:TSHACtx;sz:Word):String;implementationconstHexChars:array[0..15]ofChar=('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');//整数转换成16进制functionIntToHex(Int:Int64;IntSize:Byte):String;varn:Byte;beginResult:='';forn:=0toIntSize-1dob
3、eginResult:=HexChars[Intand$F]+Result;Int:=Intshr$4;end;end;functionror(x:LongWord;y:Byte):LongWord;assembler;asmmovcl,dlroreax,clend;functionrol(x:LongWord;y:Byte):LongWord;assembler;asmmovcl,dlroleax,clend;functionEndian(X:LongWord):LongWord;asmbswa
4、peaxend;functionft1(t:Byte;x,y,z:LongWord):LongWord;begincasetof0..19:Result:=(xandy)or((notx)andz);20..39:Result:=xxoryxorz;40..59:Result:=(xandy)or(xandz)or(yandz);elseResult:=xxoryxorz;end;end;//四个常数functionKt1(t:Byte):LongWord;begincasetof0..19:Re
5、sult:=$5a827999;20..39:Result:=$6ed9eba1;40..59:Result:=$8f1bbcdc;elseResult:=$ca62c1d6end;end;procedureSHACompress(varmd:TSHACtx);varS:array[0..4]ofLongWord;W:array[0..79]ofLongWord;i,t:LongWord;beginMove(md.state,S,SizeOf(S));fori:=0to15doW[i]:=Endi
6、an(PLongWord(LongWord(@md.buf)+i*4)^);fori:=16to79doW[i]:=rol(W[i-3]xorW[i-8]xorW[i-14]xorW[i-16],1);fori:=0to79dobegint:=rol(S[0],5)+ft1(i,S[1],S[2],S[3])+S[4]+Kt1(i)+W[i];S[4]:=S[3];S[3]:=S[2];S[2]:=rol(S[1],30);S[1]:=S[0];S[0]:=t;end;fori:=0to4domd
7、.state[i]:=md.state[i]+S[i];end;//五个32位变量procedureSHAInit(varmd:TSHACtx);beginmd.curlen:=0;md.length:=0;md.state[0]:=$67452301;md.state[1]:=$efcdab89;md.state[2]:=$98badcfe;md.state[3]:=$10325476;md.state[4]:=$c3d2e1f0;end;procedureSHAUpdate(varmd:TSH
8、ACtx;buf:Pointer;len:Int64;sz:Word);beginwhile(len>0)dobeginmd.buf[md.curlen]:=PByte(buf)^;md.curlen:=md.curlen+1;buf:=Ptr(LongWord(buf)+1);if(md.curlen=64)thenbeginSHACompress(md);md.length:=md.length+512;md.curlen:=0;end;Dec(len);end;end;fun