delphi xe 可用的MD5算法
<P><div class="codetitle"><span><U>复制代码</U></span> 代码如下:</div><div class="codebody" id="code85955"><BR>unit MD5;</P><P>interface</P>
<P>uses<BR>Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<BR>Dialogs, StdCtrls;</P>
<P>type<BR>MD5Count = array of DWORD;<BR>MD5State = array of DWORD;<BR>MD5Block = array of DWORD;<BR>MD5CBits = array of Byte;<BR>MD5Digest = array of Byte;<BR>MD5Buffer = array of Byte;</P>
<P>MD5Context = record<BR>State: MD5State;<BR>Count: MD5Count;<BR>Buffer: MD5Buffer;<BR>end;</P>
<P>procedure MD5Init(var Context: MD5Context);<BR>procedure MD5Update(var Context: MD5Context; Input: PAnsiChar;<BR>Length: longword);<BR>procedure MD5Final(var Context: MD5Context; var Digest: MD5Digest);<BR>function MD5File(N: String): MD5Digest;<BR>function MD5Print(D: MD5Digest): AnsiString;<BR>function MD5F(FileName: AnsiString): AnsiString;<BR>function MD5S(Str: AnsiString): AnsiString;</P>
<P>// MD5F为计算文件的MD5值,MD5S为计算字符串的MD5值!<BR>var<BR>PADDING: MD5Buffer = ($80, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,<BR>$00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,<BR>$00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,<BR>$00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,<BR>$00, $00, $00, $00, $00, $00, $00, $00);</P>
<P>implementation</P>
<P>function F(x, y, z: DWORD): DWORD;<BR>begin<BR>Result := (x and y) or ((not x) and z);<BR>end;</P>
<P>function G(x, y, z: DWORD): DWORD;<BR>begin<BR>Result := (x and z) or (y and (not z));<BR>end;</P>
<P>function H(x, y, z: DWORD): DWORD;<BR>begin<BR>Result := x xor y xor z;<BR>end;</P>
<P>function I(x, y, z: DWORD): DWORD;<BR>begin<BR>Result := y xor (x or (not z));<BR>end;</P>
<P>procedure rot(var x: DWORD; N: Byte);<BR>begin<BR>x := (x shl N) or (x shr (32 - N));<BR>end;</P>
<P>procedure FF(var a: DWORD; b, c, D, x: DWORD; s: Byte; ac: DWORD);<BR>begin<BR>inc(a, F(b, c, D) + x + ac);<BR>rot(a, s);<BR>inc(a, b);<BR>end;</P>
<P>procedure GG(var a: DWORD; b, c, D, x: DWORD; s: Byte; ac: DWORD);<BR>begin<BR>inc(a, G(b, c, D) + x + ac);<BR>rot(a, s);<BR>inc(a, b);<BR>end;</P>
<P>procedure HH(var a: DWORD; b, c, D, x: DWORD; s: Byte; ac: DWORD);<BR>begin<BR>inc(a, H(b, c, D) + x + ac);<BR>rot(a, s);<BR>inc(a, b);<BR>end;</P>
<P>procedure II(var a: DWORD; b, c, D, x: DWORD; s: Byte; ac: DWORD);<BR>begin<BR>inc(a, I(b, c, D) + x + ac);<BR>rot(a, s);<BR>inc(a, b);<BR>end;</P>
<P>procedure Encode(Source, Target: pointer; Count: longword);<BR>var<BR>s: PByte;<BR>T: PDWORD;<BR>I: longword;<BR>begin<BR>s := Source;<BR>T := Target;<BR>for I := 1 to Count div 4 do<BR>begin<BR>T^ := s^;<BR>inc(s);<BR>T^ := T^ or (s^ shl 8);<BR>inc(s);<BR>T^ := T^ or (s^ shl 16);<BR>inc(s);<BR>T^ := T^ or (s^ shl 24);<BR>inc(s);<BR>inc(T);<BR>end;<BR>end;</P>
<P>procedure Decode(Source, Target: pointer; Count: longword);<BR>var<BR>s: PDWORD;<BR>T: PByte;<BR>I: longword;<BR>begin<BR>s := Source;<BR>T := Target;<BR>for I := 1 to Count do<BR>begin<BR>T^ := s^ and $FF;<BR>inc(T);<BR>T^ := (s^ shr 8) and $FF;<BR>inc(T);<BR>T^ := (s^ shr 16) and $FF;<BR>inc(T);<BR>T^ := (s^ shr 24) and $FF;<BR>inc(T);<BR>inc(s);<BR>end;<BR>end;</P>
<P>procedure Transform(Buffer: pointer; var State: MD5State);<BR>var<BR>a, b, c, D: DWORD;<BR>Block: MD5Block;<BR>begin<BR>Encode(Buffer, @Block, 64);<BR>a := State;<BR>b := State;<BR>c := State;<BR>D := State;<BR>FF(a, b, c, D, Block, 7, $D76AA478);<BR>FF(D, a, b, c, Block, 12, $E8C7B756);<BR>FF(c, D, a, b, Block, 17, $242070DB);<BR>FF(b, c, D, a, Block, 22, $C1BDCEEE);<BR>FF(a, b, c, D, Block, 7, $F57C0FAF);<BR>FF(D, a, b, c, Block, 12, $4787C62A);<BR>FF(c, D, a, b, Block, 17, $A8304613);<BR>FF(b, c, D, a, Block, 22, $FD469501);<BR>FF(a, b, c, D, Block, 7, $698098D8);<BR>FF(D, a, b, c, Block, 12, $8B44F7AF);<BR>FF(c, D, a, b, Block, 17, $FFFF5BB1);<BR>FF(b, c, D, a, Block, 22, $895CD7BE);<BR>FF(a, b, c, D, Block, 7, $6B901122);<BR>FF(D, a, b, c, Block, 12, $FD987193);<BR>FF(c, D, a, b, Block, 17, $A679438E);<BR>FF(b, c, D, a, Block, 22, $49B40821);<BR>GG(a, b, c, D, Block, 5, $F61E2562);<BR>GG(D, a, b, c, Block, 9, $C040B340);<BR>GG(c, D, a, b, Block, 14, $265E5A51);<BR>GG(b, c, D, a, Block, 20, $E9B6C7AA);<BR>GG(a, b, c, D, Block, 5, $D62F105D);<BR>GG(D, a, b, c, Block, 9, $2441453);<BR>GG(c, D, a, b, Block, 14, $D8A1E681);<BR>GG(b, c, D, a, Block, 20, $E7D3FBC8);<BR>GG(a, b, c, D, Block, 5, $21E1CDE6);<BR>GG(D, a, b, c, Block, 9, $C33707D6);<BR>GG(c, D, a, b, Block, 14, $F4D50D87);<BR>GG(b, c, D, a, Block, 20, $455A14ED);<BR>GG(a, b, c, D, Block, 5, $A9E3E905);<BR>GG(D, a, b, c, Block, 9, $FCEFA3F8);<BR>GG(c, D, a, b, Block, 14, $676F02D9);<BR>GG(b, c, D, a, Block, 20, $8D2A4C8A);<BR>HH(a, b, c, D, Block, 4, $FFFA3942);<BR>HH(D, a, b, c, Block, 11, $8771F681);<BR>HH(c, D, a, b, Block, 16, $6D9D6122);<BR>HH(b, c, D, a, Block, 23, $FDE5380C);<BR>HH(a, b, c, D, Block, 4, $A4BEEA44);<BR>HH(D, a, b, c, Block, 11, $4BDECFA9);<BR>HH(c, D, a, b, Block, 16, $F6BB4B60);<BR>HH(b, c, D, a, Block, 23, $BEBFBC70);<BR>HH(a, b, c, D, Block, 4, $289B7EC6);<BR>HH(D, a, b, c, Block, 11, $EAA127FA);<BR>HH(c, D, a, b, Block, 16, $D4EF3085);<BR>HH(b, c, D, a, Block, 23, $4881D05);<BR>HH(a, b, c, D, Block, 4, $D9D4D039);<BR>HH(D, a, b, c, Block, 11, $E6DB99E5);<BR>HH(c, D, a, b, Block, 16, $1FA27CF8);<BR>HH(b, c, D, a, Block, 23, $C4AC5665);<BR>II(a, b, c, D, Block, 6, $F4292244);<BR>II(D, a, b, c, Block, 10, $432AFF97);<BR>II(c, D, a, b, Block, 15, $AB9423A7);<BR>II(b, c, D, a, Block, 21, $FC93A039);<BR>II(a, b, c, D, Block, 6, $655B59C3);<BR>II(D, a, b, c, Block, 10, $8F0CCC92);<BR>II(c, D, a, b, Block, 15, $FFEFF47D);<BR>II(b, c, D, a, Block, 21, $85845DD1);<BR>II(a, b, c, D, Block, 6, $6FA87E4F);<BR>II(D, a, b, c, Block, 10, $FE2CE6E0);<BR>II(c, D, a, b, Block, 15, $A3014314);<BR>II(b, c, D, a, Block, 21, $4E0811A1);<BR>II(a, b, c, D, Block, 6, $F7537E82);<BR>II(D, a, b, c, Block, 10, $BD3AF235);<BR>II(c, D, a, b, Block, 15, $2AD7D2BB);<BR>II(b, c, D, a, Block, 21, $EB86D391);<BR>inc(State, a);<BR>inc(State, b);<BR>inc(State, c);<BR>inc(State, D);<BR>end;</P>
<P>procedure MD5Init(var Context: MD5Context);<BR>begin<BR>with Context do<BR>begin<BR>State := $67452301;<BR>State := $EFCDAB89;<BR>State := $98BADCFE;<BR>State := $10325476;<BR>Count := 0;<BR>Count := 0;<BR>ZeroMemory(@Buffer, SizeOf(MD5Buffer));<BR>end;<BR>end;</P>
<P>procedure MD5Update(var Context: MD5Context; Input: PAnsiChar;<BR>Length: longword);<BR>var<BR>Index: longword;<BR>PartLen: longword;<BR>I: longword;<BR>begin<BR>with Context do<BR>begin<BR>Index := (Count shr 3) and $3F;<BR>inc(Count, Length shl 3);<BR>if Count < (Length shl 3) then<BR>inc(Count);<BR>inc(Count, Length shr 29);<BR>end;<BR>PartLen := 64 - Index;<BR>if Length >= PartLen then<BR>begin<BR>CopyMemory(@Context.Buffer, Input, PartLen);<BR>Transform(@Context.Buffer, Context.State);<BR>I := PartLen;<BR>while I + 63 < Length do<BR>begin<BR>Transform(@Input, Context.State);<BR>inc(I, 64);<BR>end;<BR>Index := 0;<BR>end<BR>else<BR>I := 0;<BR>CopyMemory(@Context.Buffer, @Input, Length - I);<BR>end;</P>
<P>procedure MD5Final(var Context: MD5Context; var Digest: MD5Digest);<BR>var<BR>Bits: MD5CBits;<BR>Index: longword;<BR>PadLen: longword;<BR>begin<BR>Decode(@Context.Count, @Bits, 2);<BR>Index := (Context.Count shr 3) and $3F;<BR>if Index < 56 then<BR>PadLen := 56 - Index<BR>else<BR>PadLen := 120 - Index;<BR>MD5Update(Context, @PADDING, PadLen);<BR>MD5Update(Context, @Bits, 8);<BR>Decode(@Context.State, @Digest, 4);<BR>ZeroMemory(@Context, SizeOf(MD5Context));<BR>end;</P>
<P>function MD5String(M: AnsiString): MD5Digest;<BR>var<BR>Context: MD5Context;<BR>begin<BR>MD5Init(Context);<BR>MD5Update(Context, PAnsiChar(M), Length(M));<BR>MD5Final(Context, Result);<BR>end;</P>
<P>function MD5File(N: String): MD5Digest;<BR>var<BR>FileHandle: THandle;<BR>MapHandle: THandle;<BR>ViewPointer: pointer;<BR>Context: MD5Context;<BR>begin<BR>MD5Init(Context);<BR>FileHandle := CreateFile(PWideChar(WideString(N)), GENERIC_READ,<BR>FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,<BR>FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);<BR>if FileHandle <> INVALID_HANDLE_VALUE then<BR>try<BR>MapHandle := CreateFileMapping(FileHandle, nil, PAGE_READONLY, 0, 0, nil);<BR>if MapHandle <> 0 then<BR>try<BR>ViewPointer := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 0);<BR>if ViewPointer <> nil then<BR>try<BR>MD5Update(Context, ViewPointer, GetFileSize(FileHandle, nil));<BR>finally<BR>UnmapViewOfFile(ViewPointer);<BR>end;<BR>finally<BR>CloseHandle(MapHandle);<BR>end;<BR>finally<BR>CloseHandle(FileHandle);<BR>end;<BR>MD5Final(Context, Result);<BR>end;</P>
<P>function MD5Print(D: MD5Digest): AnsiString;<BR>var<BR>I: Byte;<BR>const<BR>Digits: array of Ansichar = ('0', '1', '2', '3', '4', '5', '6', '7',<BR>'8', '9', 'a', 'b', 'c', 'd', 'e', 'f');<BR>begin<BR>Result := '';<BR>for I := 0 to 15 do<BR>Result := Result + Digits[(D shr 4) and $0F] + Digits and $0F];<BR>end;</P>
<P>function MD5Match(D1, D2: MD5Digest): boolean;<BR>var<BR>I: Byte;<BR>begin<BR>I := 0;<BR>Result := TRUE;<BR>while Result and (I < 16) do<BR>begin<BR>Result := D1 = D2;<BR>inc(I);<BR>end;<BR>end;</P>
<P>function MD5S(Str: AnsiString): AnsiString;<BR>begin<BR>Result := MD5Print(MD5String(Str));<BR>end;</P>
<P>function MD5F(FileName: AnsiString): AnsiString;<BR>begin<BR>Result := MD5Print(MD5File(string(FileName)));<BR>end;</P>
<P></div></P>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>c++实现MD5算法实现代码</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]