{$MODE OBJFPC}
unit md5;

interface

uses
  dstrnum,
  tp6md5;

type
  TMd5Digest = tp6md5.TDigest;

function Md5DigestToStr(const Digest: TMd5Digest): AnsiString;
function HashMd5(const Message: AnsiString): TMd5Digest; inline;

operator =  (const A, B: TMd5Digest): Boolean; inline;
operator <> (const A, B: TMd5Digest): Boolean; inline;
operator := (const Digest: TMd5Digest): AnsiString; inline;

implementation

function Md5DigestToStr(const Digest: TMd5Digest): AnsiString;
var
  I: LongInt;
begin
  Result := '';
  for I := Low(Digest) to High(Digest) do
    Result := Result + IntToHex(Digest[I], 2);
end;

function HashMd5(const Message: AnsiString): TMd5Digest;
begin
  tp6md5.md5digest(Message[1], Length(Message), Result);
end;

operator =  (const A, B: TMd5Digest): Boolean;
begin
  Result := CompareByte(A[0], B[0], Length(TMd5Digest)) =  0;
end;

operator <> (const A, B: TMd5Digest): Boolean;
begin
  Result := CompareByte(A[0], B[0], Length(TMd5Digest)) <> 0;
end;

operator := (const Digest: TMd5Digest): AnsiString;
begin
  Result := Md5DigestToStr(Digest);
end;

end.
