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 a String

Applies to: ~>1.0

TPJMD5 can calculate the MD5 hash of three of Delphi’s string types:

There are overloads of the TPJMD5.Calculate and TPJMD5.Process methods that can be used to create MD5 hashes of each string type.

Code examples in this how-to all use TPJMD5.Calculate because it lends itself to more concise code, but TPJMD5.Process can be used for the same purpose. For details of the differences see Understand the Calculate and Process methods.

Unicode strings

There are two overloads of TPJMD5.Calculate that deal with Unicode strings, one that takes a single Unicode string parameter and another that takes an additional TEncoding parameter.

The single parameter versions of the methods always converts the given string to its default ANSI encoding before calculating the hash. The two parameter versions apply the specified encoding to the string before calculating the hash.

To see this in action, create a new Delphi VCL forms application and drop five TEdit controls on the form. Implement the FormCreate event handler as follows:

procedure TForm1.FormCreate(Sender: TObject);
var
  D: TPJMD5Digest;
const
  S = 'Iñtërnâtiônàlizætiøn';
begin
  D := TPJMD5.Calculate(S);
  Edit1.Text := D;
  D := TPJMD5.Calculate(S, TEncoding.Default);
  Edit2.Text := D;
  D := TPJMD5.Calculate(S, TEncoding.UTF8);
  Edit3.Text := D;
  D := TPJMD5.Calculate(S, TEncoding.Unicode);
  Edit4.Text := D;
  D := TPJMD5.Calculate(S, TEncoding.ASCII);
  Edit5.Text := D;
end;

The reason why a TPJMD5Digest record can be assigned to the edit controls’ Text property is explained in How to Get a digest string.

This code places a string representation of the 5 MD5 hashes in the edit controls. On my UK English system I get:

  1. 580e27689cf5accd2f6ea979f9f3b11c - default ANSI encoding (single parameter version of TPJMD5.Calculate)
  2. 580e27689cf5accd2f6ea979f9f3b11c - explicit default ANSI encoding
  3. e5e628206e73b1ae69b37fc69762a1e1 - explicit UTF-8 encoding
  4. 0e52e06ecd5bd0c61eeb52c19263946a - explicit Unicode (little endian) encoding (UTF-16LE)
  5. 70b4903abcb62ace84264ad0443ae759 - explicit ASCII encoding

Your mileage may vary in that the first two hashes will depend on your locale and hence which ANSI code page is used for the default encoding.

Wide strings

TPJMD5 provides one overload of the TPJMD5.Calculate method that processes WideString strings. Wide strings are encoded in UTF-16 little endian (LE) format with each character occupying two bytes.

We’ll exercise TPJMD5.Calculate by creating a new Delphi VCL forms application that contains a single edit control and the following FormCreate event handler:

procedure TForm1.FormCreate(Sender: TObject);
var
  D: TPJMD5Digest;
const
  S: WideString = 'Iñtërnâtiônàlizætiøn';

begin
  D := TPJMD5.Calculate(S);
  Edit1.Text := D;
end;

Running this code gives me the result:

which is the same as result 4 in the Unicode strings section. This is as expected because UTF-16LE is used as the encoding in both cases.

Ansi strings

There’s just one overload of TPJMD5.Calculate that handles Ansi strings. It takes a single RawByteString parameter, meaning it doesn’t care what the code page of the Ansi string is.

We’ll demonstrate this by getting the MD5 hash of two Ansi strings, one with the default code page and one UTF8 string.

Create a new Delphi VCL forms application and this time drop two edit controls on the form. Create a FormCreate event handler as follows:

procedure TForm1.FormCreate(Sender: TObject);
var
  D: TPJMD5Digest;
type
  ASCIIString = type AnsiString(20127);
const
  SANSI: AnsiString = 'Iñtërnâtiônàlizætiøn';
  SUTF8: UTF8String = 'Iñtërnâtiônàlizætiøn';
  SASCII: ASCIIString = 'Iñtërnâtiônàlizætiøn';
begin
  D := TPJMD5.Calculate(SANSI);
  Edit1.Text := D;
  D := TPJMD5.Calculate(SUTF8);
  Edit2.Text := D;
  D := TPJMD5.Calculate(SASCII);
  Edit3.Text := D;
end;

When I run this program on my UK English system I get:

  1. 580e27689cf5accd2f6ea979f9f3b11c - default code page
  2. e5e628206e73b1ae69b37fc69762a1e1 - UTF8
  3. 70b4903abcb62ace84264ad0443ae759 - ASCII

Compare these with results 1 (or 2) and 3 for the Unicode string results and you will see they are the same, as you would expect.

Short strings

As with Ansi strings there is just one overload of TPJMD5.Calculate that handles ShortString strings. These strings always use the default Ansi code page.

Create a similar VCL forms application as before with just the one edit control this time. The FormCreate method is now:

procedure TForm1.FormCreate(Sender: TObject);
var
  D: TPJMD5Digest;
const
  S: ShortString = 'Iñtërnâtiônàlizætiøn';
begin
  D := TPJMD5.Calculate(S);
  Edit1.Text := D;
end;

Running the program on the expected UK English system gives the result:

As you will by now expect this is the same as the results in the previous sections for the default Ansi code page.

See Also