分享

XE 自带的 MD5 单元 MessageDigest_5.pas

 delphiXE6 2014-05-07
位于 source/Win32/soap/wsdlimporter 目录下面

例子:

uses Types, MessageDigest_5;
procedure TForm1.Button1Click(Sender: TObject);
var
  MD5: IMD5;
begin
  MD5 := GetMD5;
  MD5.Init;
  MD5.Update(TByteDynArray(RawByteString(Edit1.Text)), Length(Edit1.Text));
  Edit2.Text := LowerCase(MD5.AsString);
end;

[delphi] view plaincopy
  1. unit MD5;  
  2. interface  
  3. uses  
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  5. Dialogs, StdCtrls;  
  6. type  
  7. MD5Count = array[0..1of DWORD;  
  8. MD5State = array[0..3of DWORD;  
  9. MD5Block = array[0..15of DWORD;  
  10. MD5CBits = array[0..7of Byte;  
  11. MD5Digest = array[0..15of Byte;  
  12. MD5Buffer = array[0..63of Byte;  
  13. MD5Context = record  
  14.     State: MD5State;  
  15.     Count: MD5Count;  
  16.     Buffer: MD5Buffer;  
  17. end;  
  18. procedure MD5Init(var Context: MD5Context);  
  19. procedure MD5Update(var Context: MD5Context; Input: PAnsiChar; Length: longword);  
  20. procedure MD5Final(var Context: MD5Context; var Digest: MD5Digest);  
  21. function MD5File(N:String): MD5Digest;  
  22. function MD5Print(D: MD5Digest): AnsiString;  
  23. function MD5F(FileName:AnsiString): AnsiString;  
  24. function MD5S(Str: AnsiString): AnsiString;  
  25. //MD5F为计算文件的MD5值,MD5S为计算字符串的MD5值!  
  26. var  
  27. PADDING: MD5Buffer = (  
  28.     $80$00$00$00$00$00$00$00,  
  29.     $00$00$00$00$00$00$00$00,  
  30.     $00$00$00$00$00$00$00$00,  
  31.     $00$00$00$00$00$00$00$00,  
  32.     $00$00$00$00$00$00$00$00,  
  33.     $00$00$00$00$00$00$00$00,  
  34.     $00$00$00$00$00$00$00$00,  
  35.     $00$00$00$00$00$00$00$00);  
  36. implementation  
  37. function F(x, y, z: DWORD): DWORD;  
  38. begin  
  39. Result := (x and y) or ((not x) and z);  
  40. end;  
  41. function G(x, y, z: DWORD): DWORD;  
  42. begin  
  43. Result := (x and z) or (y and (not z));  
  44. end;  
  45. function H(x, y, z: DWORD): DWORD;  
  46. begin  
  47. Result := x xor y xor z;  
  48. end;  
  49. function I(x, y, z: DWORD): DWORD;  
  50. begin  
  51. Result := y xor (x or (not z));  
  52. end;  
  53. procedure rot(var x: DWORD; n: BYTE);  
  54. begin  
  55. x := (x shl n) or (x shr (32 - n));  
  56. end;  
  57. procedure FF(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);  
  58. begin  
  59. inc(a, F(b, c, d) + x + ac);  
  60. rot(a, s);  
  61. inc(a, b);  
  62. end;  
  63. procedure GG(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);  
  64. begin  
  65. inc(a, G(b, c, d) + x + ac);  
  66. rot(a, s);  
  67. inc(a, b);  
  68. end;  
  69. procedure HH(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);  
  70. begin  
  71. inc(a, H(b, c, d) + x + ac);  
  72. rot(a, s);  
  73. inc(a, b);  
  74. end;  
  75. procedure II(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);  
  76. begin  
  77. inc(a, I(b, c, d) + x + ac);  
  78. rot(a, s);  
  79. inc(a, b);  
  80. end;  
  81. procedure Encode(Source, Target: pointer; Count: longword);  
  82. var  
  83. S: PByte;  
  84. T: PDWORD;  
  85. I: longword;  
  86. begin  
  87. S := Source;  
  88. T := Target;  
  89. for I := 1 to Count div 4 do begin  
  90.     T^ := S^;  
  91.     inc(S);  
  92.     T^ := T^ or (S^ shl 8);  
  93.     inc(S);  
  94.     T^ := T^ or (S^ shl 16);  
  95.     inc(S);  
  96.     T^ := T^ or (S^ shl 24);  
  97.     inc(S);  
  98.     inc(T);  
  99. end;  
  100. end;  
  101. procedure Decode(Source, Target: pointer; Count: longword);  
  102. var  
  103. S: PDWORD;  
  104. T: PByte;  
  105. I: longword;  
  106. begin  
  107. S := Source;  
  108. T := Target;  
  109. for I := 1 to Count do begin  
  110.     T^ := S^ and $ff;  
  111.     inc(T);  
  112.     T^ := (S^ shr 8and $ff;  
  113.     inc(T);  
  114.     T^ := (S^ shr 16and $ff;  
  115.     inc(T);  
  116.     T^ := (S^ shr 24and $ff;  
  117.     inc(T);  
  118.     inc(S);  
  119. end;  
  120. end;  
  121. procedure Transform(Buffer: pointervar State: MD5State);  
  122. var  
  123. a, b, c, d: DWORD;  
  124. Block: MD5Block;  
  125. begin  
  126. Encode(Buffer, @Block, 64);  
  127. a := State[0];  
  128. b := State[1];  
  129. c := State[2];  
  130. d := State[3];  
  131. FF (a, b, c, d, Block[ 0], 7$d76aa478);  
  132. FF (d, a, b, c, Block[ 1], 12$e8c7b756);  
  133. FF (c, d, a, b, Block[ 2], 17$242070db);  
  134. FF (b, c, d, a, Block[ 3], 22$c1bdceee);  
  135. FF (a, b, c, d, Block[ 4], 7$f57c0faf);  
  136. FF (d, a, b, c, Block[ 5], 12$4787c62a);  
  137. FF (c, d, a, b, Block[ 6], 17$a8304613);  
  138. FF (b, c, d, a, Block[ 7], 22$fd469501);  
  139. FF (a, b, c, d, Block[ 8], 7$698098d8);  
  140. FF (d, a, b, c, Block[ 9], 12$8b44f7af);  
  141. FF (c, d, a, b, Block[10], 17$ffff5bb1);  
  142. FF (b, c, d, a, Block[11], 22$895cd7be);  
  143. FF (a, b, c, d, Block[12], 7$6b901122);  
  144. FF (d, a, b, c, Block[13], 12$fd987193);  
  145. FF (c, d, a, b, Block[14], 17$a679438e);  
  146. FF (b, c, d, a, Block[15], 22$49b40821);  
  147. GG (a, b, c, d, Block[ 1], 5$f61e2562);  
  148. GG (d, a, b, c, Block[ 6], 9$c040b340);  
  149. GG (c, d, a, b, Block[11], 14$265e5a51);  
  150. GG (b, c, d, a, Block[ 0], 20$e9b6c7aa);  
  151. GG (a, b, c, d, Block[ 5], 5$d62f105d);  
  152. GG (d, a, b, c, Block[10], 9$2441453);  
  153. GG (c, d, a, b, Block[15], 14$d8a1e681);  
  154. GG (b, c, d, a, Block[ 4], 20$e7d3fbc8);  
  155. GG (a, b, c, d, Block[ 9], 5$21e1cde6);  
  156. GG (d, a, b, c, Block[14], 9$c33707d6);  
  157. GG (c, d, a, b, Block[ 3], 14$f4d50d87);  
  158. GG (b, c, d, a, Block[ 8], 20$455a14ed);  
  159. GG (a, b, c, d, Block[13], 5$a9e3e905);  
  160. GG (d, a, b, c, Block[ 2], 9$fcefa3f8);  
  161. GG (c, d, a, b, Block[ 7], 14$676f02d9);  
  162. GG (b, c, d, a, Block[12], 20$8d2a4c8a);  
  163. HH (a, b, c, d, Block[ 5], 4$fffa3942);  
  164. HH (d, a, b, c, Block[ 8], 11$8771f681);  
  165. HH (c, d, a, b, Block[11], 16$6d9d6122);  
  166. HH (b, c, d, a, Block[14], 23$fde5380c);  
  167. HH (a, b, c, d, Block[ 1], 4$a4beea44);  
  168. HH (d, a, b, c, Block[ 4], 11$4bdecfa9);  
  169. HH (c, d, a, b, Block[ 7], 16$f6bb4b60);  
  170. HH (b, c, d, a, Block[10], 23$bebfbc70);  
  171. HH (a, b, c, d, Block[13], 4$289b7ec6);  
  172. HH (d, a, b, c, Block[ 0], 11$eaa127fa);  
  173. HH (c, d, a, b, Block[ 3], 16$d4ef3085);  
  174. HH (b, c, d, a, Block[ 6], 23$4881d05);  
  175. HH (a, b, c, d, Block[ 9], 4$d9d4d039);  
  176. HH (d, a, b, c, Block[12], 11$e6db99e5);  
  177. HH (c, d, a, b, Block[15], 16$1fa27cf8);  
  178. HH (b, c, d, a, Block[ 2], 23$c4ac5665);  
  179. II (a, b, c, d, Block[ 0], 6$f4292244);  
  180. II (d, a, b, c, Block[ 7], 10$432aff97);  
  181. II (c, d, a, b, Block[14], 15$ab9423a7);  
  182. II (b, c, d, a, Block[ 5], 21$fc93a039);  
  183. II (a, b, c, d, Block[12], 6$655b59c3);  
  184. II (d, a, b, c, Block[ 3], 10$8f0ccc92);  
  185. II (c, d, a, b, Block[10], 15$ffeff47d);  
  186. II (b, c, d, a, Block[ 1], 21$85845dd1);  
  187. II (a, b, c, d, Block[ 8], 6$6fa87e4f);  
  188. II (d, a, b, c, Block[15], 10$fe2ce6e0);  
  189. II (c, d, a, b, Block[ 6], 15$a3014314);  
  190. II (b, c, d, a, Block[13], 21$4e0811a1);  
  191. II (a, b, c, d, Block[ 4], 6$f7537e82);  
  192. II (d, a, b, c, Block[11], 10$bd3af235);  
  193. II (c, d, a, b, Block[ 2], 15$2ad7d2bb);  
  194. II (b, c, d, a, Block[ 9], 21$eb86d391);  
  195. inc(State[0], a);  
  196. inc(State[1], b);  
  197. inc(State[2], c);  
  198. inc(State[3], d);  
  199. end;  
  200. procedure MD5Init(var Context: MD5Context);  
  201. begin  
  202. with Context do begin  
  203.     State[0] := $67452301;  
  204.     State[1] := $efcdab89;  
  205.     State[2] := $98badcfe;  
  206.     State[3] := $10325476;  
  207.     Count[0] := 0;  
  208.     Count[1] := 0;  
  209.     ZeroMemory(@Buffer, SizeOf(MD5Buffer));  
  210. end;  
  211. end;  
  212. procedure MD5Update(var Context: MD5Context; Input: PAnsiChar; Length: longword);  
  213. var  
  214. Index: longword;  
  215. PartLen: longword;  
  216. I: longword;  
  217. begin  
  218. with Context do begin  
  219.     Index := (Count[0shr 3and $3f;  
  220.     inc(Count[0], Length shl 3);  
  221.     if Count[0] < (Length shl 3then inc(Count[1]);  
  222.     inc(Count[1], Length shr 29);  
  223. end;  
  224. PartLen := 64 - Index;  
  225. if Length >= PartLen then begin  
  226.     CopyMemory(@Context.Buffer[Index], Input, PartLen);  
  227.     Transform(@Context.Buffer, Context.State);  
  228.     I := PartLen;  
  229.     while I + 63 < Length do begin  
  230.       Transform(@Input[I], Context.State);  
  231.       inc(I, 64);  
  232.     end;  
  233.     Index := 0;  
  234. end else I := 0;  
  235. CopyMemory(@Context.Buffer[Index], @Input[I], Length - I);  
  236. end;  
  237. procedure MD5Final(var Context: MD5Context; var Digest: MD5Digest);  
  238. var  
  239. Bits: MD5CBits;  
  240. Index: longword;  
  241. PadLen: longword;  
  242. begin  
  243. Decode(@Context.Count, @Bits, 2);  
  244. Index := (Context.Count[0shr 3and $3f;  
  245. if Index < 56 then PadLen := 56 - Index else PadLen := 120 - Index;  
  246. MD5Update(Context, @PADDING, PadLen);  
  247. MD5Update(Context, @Bits, 8);  
  248. Decode(@Context.State, @Digest, 4);  
  249. ZeroMemory(@Context, SizeOf(MD5Context));  
  250. end;  
  251. function MD5String(M: AnsiString): MD5Digest;  
  252. var  
  253. Context: MD5Context;  
  254. begin  
  255. MD5Init(Context);  
  256. MD5Update(Context, PAnsiChar(M), length(M));  
  257. MD5Final(Context, Result);  
  258. end;  
  259. function MD5File(N:String): MD5Digest;  
  260. var  
  261. FileHandle: THandle;  
  262. MapHandle: THandle;  
  263. ViewPointer: pointer;  
  264. Context: MD5Context;  
  265. begin  
  266. MD5Init(Context);  
  267. FileHandle := CreateFile(PWideChar(WideString(N)), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE,  
  268.     nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);  
  269. if FileHandle <> INVALID_HANDLE_VALUE then try  
  270.     MapHandle := CreateFileMapping(FileHandle, nil, PAGE_READONLY, 00nil);  
  271.     if MapHandle <> 0 then try  
  272.       ViewPointer := MapViewOfFile(MapHandle, FILE_MAP_READ, 000);  
  273.       if ViewPointer <> nil then try  
  274.         MD5Update(Context, ViewPointer, GetFileSize(FileHandle, nil));  
  275.       finally  
  276.         UnmapViewOfFile(ViewPointer);  
  277.       end;  
  278.     finally  
  279.       CloseHandle(MapHandle);  
  280.     end;  
  281. finally  
  282.     CloseHandle(FileHandle);  
  283. end;  
  284. MD5Final(Context, Result);  
  285. end;  
  286. function MD5Print(D: MD5Digest): AnsiString;  
  287. var  
  288. I: byte;  
  289. const  
  290. Digits: array[0..15of Ansichar =  
  291.     ('0''1''2''3''4''5''6''7''8''9''a''b''c''d''e''f');  
  292. begin  
  293. Result := '';  
  294. for I := 0 to 15 do Result := Result + Digits[(D[I] shr 4and $0f] + Digits[D[I] and $0f];  
  295. end;  
  296. function MD5Match(D1, D2: MD5Digest): boolean;  
  297. var  
  298. I: byte;  
  299. begin  
  300. I := 0;  
  301. Result := TRUE;  
  302. while Result and (I < 16do begin  
  303.     Result := D1[I] = D2[I];  
  304.     inc(I);  
  305. end;  
  306. end;  
  307. function MD5S(Str: AnsiString): AnsiString;  
  308. begin  
  309. Result := MD5Print(MD5String(Str));  
  310. end;  
  311. function MD5F(FileName: AnsiString): AnsiString;  
  312. begin  
  313. Result := MD5Print(MD5File(string(FileName)));  
  314. end;  
  315. end.  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多