{"id":378,"date":"2009-05-08T20:54:00","date_gmt":"2009-05-08T12:54:00","guid":{"rendered":""},"modified":"2013-11-17T19:25:20","modified_gmt":"2013-11-17T11:25:20","slug":"%e9%80%82%e5%90%88delphi2009%e7%9a%84md5%e5%8d%95%e5%85%83","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/378.html","title":{"rendered":"\u9002\u5408Delphi2009\u7684MD5\u5355\u5143"},"content":{"rendered":"<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nunit MD5;\r\n\r\ninterface\r\nuses\r\nWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,\r\nDialogs, StdCtrls;\r\ntype\r\nMD5Count = array&#x5B;0..1] of DWORD;\r\nMD5State = array&#x5B;0..3] of DWORD;\r\nMD5Block = array&#x5B;0..15] of DWORD;\r\nMD5CBits = array&#x5B;0..7] of Byte;\r\nMD5Digest = array&#x5B;0..15] of Byte;\r\nMD5Buffer = array&#x5B;0..63] of Byte;\r\nMD5Context = record\r\n    State: MD5State;\r\n    Count: MD5Count;\r\n    Buffer: MD5Buffer;\r\nend;\r\n\r\nprocedure MD5Init(var Context: MD5Context);\r\nprocedure MD5Update(var Context: MD5Context; Input: PAnsiChar; Length: longword);\r\nprocedure MD5Final(var Context: MD5Context; var Digest: MD5Digest);\r\nfunction MD5File(N:String): MD5Digest;\r\nfunction MD5Print(D: MD5Digest): AnsiString;\r\nfunction MD5F(FileName:AnsiString): AnsiString;\r\nfunction MD5S(Str: AnsiString): AnsiString;\r\n\/\/MD5F\u4e3a\u8ba1\u7b97\u6587\u4ef6\u7684MD5\u503c,MD5S\u4e3a\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684MD5\u503c!\r\nvar\r\nPADDING: MD5Buffer = (\r\n    $80, $00, $00, $00, $00, $00, $00, $00,\r\n    $00, $00, $00, $00, $00, $00, $00, $00,\r\n    $00, $00, $00, $00, $00, $00, $00, $00,\r\n    $00, $00, $00, $00, $00, $00, $00, $00,\r\n    $00, $00, $00, $00, $00, $00, $00, $00,\r\n    $00, $00, $00, $00, $00, $00, $00, $00,\r\n    $00, $00, $00, $00, $00, $00, $00, $00,\r\n    $00, $00, $00, $00, $00, $00, $00, $00);\r\nimplementation\r\n\r\nfunction F(x, y, z: DWORD): DWORD;\r\nbegin\r\nResult := (x and y) or ((not x) and z);\r\nend;\r\n\r\nfunction G(x, y, z: DWORD): DWORD;\r\nbegin\r\nResult := (x and z) or (y and (not z));\r\nend;\r\n\r\nfunction H(x, y, z: DWORD): DWORD;\r\nbegin\r\nResult := x xor y xor z;\r\nend;\r\n\r\nfunction I(x, y, z: DWORD): DWORD;\r\nbegin\r\nResult := y xor (x or (not z));\r\nend;\r\n\r\nprocedure rot(var x: DWORD; n: BYTE);\r\nbegin\r\nx := (x shl n) or (x shr (32 - n));\r\nend;\r\n\r\nprocedure FF(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);\r\nbegin\r\ninc(a, F(b, c, d) + x + ac);\r\nrot(a, s);\r\ninc(a, b);\r\nend;\r\n\r\nprocedure GG(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);\r\nbegin\r\ninc(a, G(b, c, d) + x + ac);\r\nrot(a, s);\r\ninc(a, b);\r\nend;\r\n\r\nprocedure HH(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);\r\nbegin\r\ninc(a, H(b, c, d) + x + ac);\r\nrot(a, s);\r\ninc(a, b);\r\nend;\r\n\r\nprocedure II(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);\r\nbegin\r\ninc(a, I(b, c, d) + x + ac);\r\nrot(a, s);\r\ninc(a, b);\r\nend;\r\n\r\nprocedure Encode(Source, Target: pointer; Count: longword);\r\nvar\r\nS: PByte;\r\nT: PDWORD;\r\nI: longword;\r\nbegin\r\nS := Source;\r\nT := Target;\r\nfor I := 1 to Count div 4 do begin\r\n    T^ := S^;\r\n    inc(S);\r\n    T^ := T^ or (S^ shl 8);\r\n    inc(S);\r\n    T^ := T^ or (S^ shl 16);\r\n    inc(S);\r\n    T^ := T^ or (S^ shl 24);\r\n    inc(S);\r\n    inc(T);\r\nend;\r\nend;\r\n\r\nprocedure Decode(Source, Target: pointer; Count: longword);\r\nvar\r\nS: PDWORD;\r\nT: PByte;\r\nI: longword;\r\nbegin\r\nS := Source;\r\nT := Target;\r\nfor I := 1 to Count do begin\r\n    T^ := S^ and $ff;\r\n    inc(T);\r\n    T^ := (S^ shr 8) and $ff;\r\n    inc(T);\r\n    T^ := (S^ shr 16) and $ff;\r\n    inc(T);\r\n    T^ := (S^ shr 24) and $ff;\r\n    inc(T);\r\n    inc(S);\r\nend;\r\nend;\r\n\r\nprocedure Transform(Buffer: pointer; var State: MD5State);\r\nvar\r\na, b, c, d: DWORD;\r\nBlock: MD5Block;\r\nbegin\r\nEncode(Buffer, @Block, 64);\r\na := State&#x5B;0];\r\nb := State&#x5B;1];\r\nc := State&#x5B;2];\r\nd := State&#x5B;3];\r\nFF (a, b, c, d, Block&#x5B; 0], 7, $d76aa478);\r\nFF (d, a, b, c, Block&#x5B; 1], 12, $e8c7b756);\r\nFF (c, d, a, b, Block&#x5B; 2], 17, $242070db);\r\nFF (b, c, d, a, Block&#x5B; 3], 22, $c1bdceee);\r\nFF (a, b, c, d, Block&#x5B; 4], 7, $f57c0faf);\r\nFF (d, a, b, c, Block&#x5B; 5], 12, $4787c62a);\r\nFF (c, d, a, b, Block&#x5B; 6], 17, $a8304613);\r\nFF (b, c, d, a, Block&#x5B; 7], 22, $fd469501);\r\nFF (a, b, c, d, Block&#x5B; 8], 7, $698098d8);\r\nFF (d, a, b, c, Block&#x5B; 9], 12, $8b44f7af);\r\nFF (c, d, a, b, Block&#x5B;10], 17, $ffff5bb1);\r\nFF (b, c, d, a, Block&#x5B;11], 22, $895cd7be);\r\nFF (a, b, c, d, Block&#x5B;12], 7, $6b901122);\r\nFF (d, a, b, c, Block&#x5B;13], 12, $fd987193);\r\nFF (c, d, a, b, Block&#x5B;14], 17, $a679438e);\r\nFF (b, c, d, a, Block&#x5B;15], 22, $49b40821);\r\nGG (a, b, c, d, Block&#x5B; 1], 5, $f61e2562);\r\nGG (d, a, b, c, Block&#x5B; 6], 9, $c040b340);\r\nGG (c, d, a, b, Block&#x5B;11], 14, $265e5a51);\r\nGG (b, c, d, a, Block&#x5B; 0], 20, $e9b6c7aa);\r\nGG (a, b, c, d, Block&#x5B; 5], 5, $d62f105d);\r\nGG (d, a, b, c, Block&#x5B;10], 9, $2441453);\r\nGG (c, d, a, b, Block&#x5B;15], 14, $d8a1e681);\r\nGG (b, c, d, a, Block&#x5B; 4], 20, $e7d3fbc8);\r\nGG (a, b, c, d, Block&#x5B; 9], 5, $21e1cde6);\r\nGG (d, a, b, c, Block&#x5B;14], 9, $c33707d6);\r\nGG (c, d, a, b, Block&#x5B; 3], 14, $f4d50d87);\r\nGG (b, c, d, a, Block&#x5B; 8], 20, $455a14ed);\r\nGG (a, b, c, d, Block&#x5B;13], 5, $a9e3e905);\r\nGG (d, a, b, c, Block&#x5B; 2], 9, $fcefa3f8);\r\nGG (c, d, a, b, Block&#x5B; 7], 14, $676f02d9);\r\nGG (b, c, d, a, Block&#x5B;12], 20, $8d2a4c8a);\r\nHH (a, b, c, d, Block&#x5B; 5], 4, $fffa3942);\r\nHH (d, a, b, c, Block&#x5B; 8], 11, $8771f681);\r\nHH (c, d, a, b, Block&#x5B;11], 16, $6d9d6122);\r\nHH (b, c, d, a, Block&#x5B;14], 23, $fde5380c);\r\nHH (a, b, c, d, Block&#x5B; 1], 4, $a4beea44);\r\nHH (d, a, b, c, Block&#x5B; 4], 11, $4bdecfa9);\r\nHH (c, d, a, b, Block&#x5B; 7], 16, $f6bb4b60);\r\nHH (b, c, d, a, Block&#x5B;10], 23, $bebfbc70);\r\nHH (a, b, c, d, Block&#x5B;13], 4, $289b7ec6);\r\nHH (d, a, b, c, Block&#x5B; 0], 11, $eaa127fa);\r\nHH (c, d, a, b, Block&#x5B; 3], 16, $d4ef3085);\r\nHH (b, c, d, a, Block&#x5B; 6], 23, $4881d05);\r\nHH (a, b, c, d, Block&#x5B; 9], 4, $d9d4d039);\r\nHH (d, a, b, c, Block&#x5B;12], 11, $e6db99e5);\r\nHH (c, d, a, b, Block&#x5B;15], 16, $1fa27cf8);\r\nHH (b, c, d, a, Block&#x5B; 2], 23, $c4ac5665);\r\nII (a, b, c, d, Block&#x5B; 0], 6, $f4292244);\r\nII (d, a, b, c, Block&#x5B; 7], 10, $432aff97);\r\nII (c, d, a, b, Block&#x5B;14], 15, $ab9423a7);\r\nII (b, c, d, a, Block&#x5B; 5], 21, $fc93a039);\r\nII (a, b, c, d, Block&#x5B;12], 6, $655b59c3);\r\nII (d, a, b, c, Block&#x5B; 3], 10, $8f0ccc92);\r\nII (c, d, a, b, Block&#x5B;10], 15, $ffeff47d);\r\nII (b, c, d, a, Block&#x5B; 1], 21, $85845dd1);\r\nII (a, b, c, d, Block&#x5B; 8], 6, $6fa87e4f);\r\nII (d, a, b, c, Block&#x5B;15], 10, $fe2ce6e0);\r\nII (c, d, a, b, Block&#x5B; 6], 15, $a3014314);\r\nII (b, c, d, a, Block&#x5B;13], 21, $4e0811a1);\r\nII (a, b, c, d, Block&#x5B; 4], 6, $f7537e82);\r\nII (d, a, b, c, Block&#x5B;11], 10, $bd3af235);\r\nII (c, d, a, b, Block&#x5B; 2], 15, $2ad7d2bb);\r\nII (b, c, d, a, Block&#x5B; 9], 21, $eb86d391);\r\ninc(State&#x5B;0], a);\r\ninc(State&#x5B;1], b);\r\ninc(State&#x5B;2], c);\r\ninc(State&#x5B;3], d);\r\nend;\r\n\r\nprocedure MD5Init(var Context: MD5Context);\r\nbegin\r\nwith Context do begin\r\n    State&#x5B;0] := $67452301;\r\n    State&#x5B;1] := $efcdab89;\r\n    State&#x5B;2] := $98badcfe;\r\n    State&#x5B;3] := $10325476;\r\n    Count&#x5B;0] := 0;\r\n    Count&#x5B;1] := 0;\r\n    ZeroMemory(@Buffer, SizeOf(MD5Buffer));\r\nend;\r\nend;\r\n\r\nprocedure MD5Update(var Context: MD5Context; Input: PAnsiChar; Length: longword);\r\nvar\r\nIndex: longword;\r\nPartLen: longword;\r\nI: longword;\r\nbegin\r\nwith Context do begin\r\n    Index := (Count&#x5B;0] shr 3) and $3f;\r\n    inc(Count&#x5B;0], Length shl 3);\r\n    if Count&#x5B;0] &lt; (Length shl 3) then inc(Count&#x5B;1]);\r\n    inc(Count&#x5B;1], Length shr 29);\r\nend;\r\nPartLen := 64 - Index;\r\nif Length &gt;= PartLen then begin\r\n    CopyMemory(@Context.Buffer&#x5B;Index], Input, PartLen);\r\n    Transform(@Context.Buffer, Context.State);\r\n    I := PartLen;\r\n    while I + 63 &lt; Length do begin\r\n      Transform(@Input&#x5B;I], Context.State);\r\n      inc(I, 64);\r\n    end;\r\n    Index := 0;\r\nend else I := 0;\r\nCopyMemory(@Context.Buffer&#x5B;Index], @Input&#x5B;I], Length - I);\r\nend;\r\n\r\nprocedure MD5Final(var Context: MD5Context; var Digest: MD5Digest);\r\nvar\r\nBits: MD5CBits;\r\nIndex: longword;\r\nPadLen: longword;\r\nbegin\r\nDecode(@Context.Count, @Bits, 2);\r\nIndex := (Context.Count&#x5B;0] shr 3) and $3f;\r\nif Index &lt; 56 then PadLen := 56 - Index else PadLen := 120 - Index;\r\nMD5Update(Context, @PADDING, PadLen);\r\nMD5Update(Context, @Bits, 8);\r\nDecode(@Context.State, @Digest, 4);\r\nZeroMemory(@Context, SizeOf(MD5Context));\r\nend;\r\n\r\nfunction MD5String(M: AnsiString): MD5Digest;\r\nvar\r\nContext: MD5Context;\r\nbegin\r\nMD5Init(Context);\r\nMD5Update(Context, PAnsiChar(M), length(M));\r\nMD5Final(Context, Result);\r\nend;\r\n\r\nfunction MD5File(N:String): MD5Digest;\r\nvar\r\nFileHandle: THandle;\r\nMapHandle: THandle;\r\nViewPointer: pointer;\r\nContext: MD5Context;\r\nbegin\r\nMD5Init(Context);\r\nFileHandle := CreateFile(PWideChar(WideString(N)), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE,\r\n    nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);\r\nif FileHandle &lt;&gt; INVALID_HANDLE_VALUE then try\r\n    MapHandle := CreateFileMapping(FileHandle, nil, PAGE_READONLY, 0, 0, nil);\r\n    if MapHandle &lt;&gt; 0 then try\r\n      ViewPointer := MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 0);\r\n      if ViewPointer &lt;&gt; nil then try\r\n\r\n        MD5Update(Context, ViewPointer, GetFileSize(FileHandle, nil));\r\n      finally\r\n        UnmapViewOfFile(ViewPointer);\r\n      end;\r\n    finally\r\n      CloseHandle(MapHandle);\r\n    end;\r\nfinally\r\n    CloseHandle(FileHandle);\r\nend;\r\nMD5Final(Context, Result);\r\nend;\r\n\r\nfunction MD5Print(D: MD5Digest): AnsiString;\r\nvar\r\nI: byte;\r\nconst\r\nDigits: array&#x5B;0..15] of Ansichar =\r\n    ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');\r\nbegin\r\nResult := '';\r\nfor I := 0 to 15 do Result := Result + Digits&#x5B;(D&#x5B;I] shr 4) and $0f] + Digits&#x5B;D&#x5B;I] and $0f];\r\nend;\r\n\r\nfunction MD5Match(D1, D2: MD5Digest): boolean;\r\nvar\r\nI: byte;\r\nbegin\r\nI := 0;\r\nResult := TRUE;\r\nwhile Result and (I &lt; 16) do begin\r\n    Result := D1&#x5B;I] = D2&#x5B;I];\r\n    inc(I);\r\nend;\r\nend;\r\n\r\nfunction MD5S(Str: AnsiString): AnsiString;\r\nbegin\r\nResult := MD5Print(MD5String(Str));\r\nend;\r\n\r\nfunction MD5F(FileName: AnsiString): AnsiString;\r\nbegin\r\nResult := MD5Print(MD5File(string(FileName)));\r\nend;\r\n\r\nend.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>unit MD5; interface uses Windows, Messages, SysUtils, V [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-378","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=378"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/378\/revisions"}],"predecessor-version":[{"id":4764,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/378\/revisions\/4764"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}