Welcome to the new DelphiDabbler Code Library Documentation.

This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.

Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.

MD5 How-to: How To Get the MD5 Hash of Ordinal Types

Applies to: ~>1.0

TPJMD5 provides no direct means of getting the MD5 hash of ordinal types. Instead we need to use the untyped overloads of the TPJMD5.Calculate and TPJMD5.Process methods.

Working with untyped data is covered in How To Get the MD5 Hash of Untyped Data

The first example displays the MD5 hash of an Int64 data type:

var
  D: TPJMD5Digest;
  I: Int64;
begin
  I := -1234567890123456789;
  D := TPJMD5.Calculate(I, SizeOf(I));
  ShowMessage(D);  // relies on implicit cast of TPJMD5Digest to string
end;

The reason why a TPJMD5Digest record can be passed to ShowMessage where a string argument is expected is explained here.

We use the untyped overload of TPJMD5.Calculate by passing Int64 variable I as the first parameter and the size of I in bytes as the second parameter.

This illustrates the general principle for getting the hash of any ordinal type: pass the ordinal variable as the first parameter to either TPJMD5.Calculate and TPJMD5.Process and pass the size of the variable as the second parameter.

The following example illustrates how this works with TPJMD5.Process - we get the MD5 hash of a Word, an Integer, and an enumerated type.

type
  TDays = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
var
  MD5: TPJMD5;
  W: Word;
  I: Integer;
  D: TDays;
begin
  W := 42;
  I := -42;
  D := Thu;
  MD5 := TPJMD5.Create;
  try
    MD5.Process(W, SizeOf(W));
    MD5.Process(I, SizeOf(I));
    MD5.Process(D, SizeOf(D));
    ShowMessage(MD5.Digest);
  finally
    MD5.Free;
  end;
end;

See Also