MD5.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. unit MD5;
  2. {
  3. MD5.pas: System.Hash.pas wrapper in the style of the old version of this file:
  4. MD5.pas: Translated from C to Delphi by Jordan Russell on 2004-03-16.
  5. Still in the public domain. The original C code was taken from dpkg.
  6. }
  7. (*
  8. * This code implements the MD5 message-digest algorithm.
  9. * The algorithm is due to Ron Rivest. This code was
  10. * written by Colin Plumb in 1993, no copyright is claimed.
  11. * This code is in the public domain; do with it what you wish.
  12. *
  13. * Equivalent code is available from RSA Data Security, Inc.
  14. * This code has been tested against that, and is equivalent,
  15. * except that you don't need to include two pages of legalese
  16. * with every copy.
  17. *
  18. * To compute the message digest of a chunk of bytes, declare an
  19. * MD5Context structure, pass it to MD5Init, call MD5Update as
  20. * needed on buffers full of bytes, and then call MD5Final, which
  21. * will fill a supplied 16-byte array with the digest.
  22. *
  23. * Changed so as no longer to depend on Colin Plumb's `usual.h' header
  24. * definitions; now uses stuff from dpkg's config.h.
  25. * - Ian Jackson <[email protected]>.
  26. * Still in the public domain.
  27. *)
  28. interface
  29. uses
  30. System.Hash;
  31. type
  32. TMD5Context = record
  33. hash: THashMD5;
  34. end;
  35. TMD5Digest = array[0..15] of Byte;
  36. procedure MD5Init(var ctx: TMD5Context);
  37. procedure MD5Update(var ctx: TMD5Context; const buffer; len: Cardinal);
  38. function MD5Final(var ctx: TMD5Context): TMD5Digest;
  39. function MD5Buf(const Buffer; Len: Cardinal): TMD5Digest;
  40. function MD5DigestsEqual(const A, B: TMD5Digest): Boolean;
  41. function MD5DigestToString(const D: TMD5Digest): String;
  42. implementation
  43. procedure MD5Init(var ctx: TMD5Context);
  44. begin
  45. ctx.hash := THashMD5.Create;
  46. end;
  47. procedure MD5Update(var ctx: TMD5Context; const buffer; len: Cardinal);
  48. begin
  49. ctx.hash.Update(buffer, len);
  50. end;
  51. function MD5Final(var ctx: TMD5Context): TMD5Digest;
  52. begin
  53. var HashAsBytes := ctx.hash.HashAsBytes;
  54. Move(HashAsBytes[0], Result[0], SizeOf(Result));
  55. end;
  56. { New functions by JR: }
  57. function MD5Buf(const Buffer; Len: Cardinal): TMD5Digest;
  58. var
  59. Context: TMD5Context;
  60. begin
  61. MD5Init(Context);
  62. MD5Update(Context, Buffer, Len);
  63. Result := MD5Final(Context);
  64. end;
  65. function MD5DigestsEqual(const A, B: TMD5Digest): Boolean;
  66. var
  67. I: Integer;
  68. begin
  69. for I := Low(TMD5Digest) to High(TMD5Digest) do
  70. if A[I] <> B[I] then begin
  71. Result := False;
  72. Exit;
  73. end;
  74. Result := True;
  75. end;
  76. function MD5DigestToString(const D: TMD5Digest): String;
  77. const
  78. Digits: array[0..15] of Char = '0123456789abcdef';
  79. var
  80. Buf: array[0..31] of Char;
  81. P: PChar;
  82. I: Integer;
  83. begin
  84. P := @Buf[0];
  85. for I := 0 to 15 do begin
  86. P^ := Digits[D[I] shr 4];
  87. Inc(P);
  88. P^ := Digits[D[I] and 15];
  89. Inc(P);
  90. end;
  91. SetString(Result, Buf, 32);
  92. end;
  93. end.