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.
Applies to: ~>1.0
You often see MD5 hashes or digests displayed as strings like this: c8b029b7698b23a5962e7cc21a75653a
These strings are always 32 characters long and are made up hexadecimal characters representing the 16 bytes of a MD5 digest.
It’s easy to get a MD5 hash string from a TPJMD5Digest record. You just assign it to a string variable:
var
D: TPJMD5Digest;
S: string;
begin
D := TPJMD5.Calculate('Foo');
S := D;
ShowMessage(S);
end;
This code works because TPJMD5Digest has an Implicit cast operator that automatically creates a string representation of the hash when it is assigned to a string.
This conversion also works when a digest record is used in a string context, such as when passed as a string parameter to a routine or method. For example:
var
D: TPJMD5Digest;
begin
D := TPJMD5.Calculate('Foo');
ShowMessage(D);
end;
This works because ShowMessage takes a string parameter.
You can also explicitly cast a TPJMD5Digest record to a string. This is useful when it is passed as a parameter to something like the array of const
parameter of routines like Format and ShowMessageFmt where the type is not recognised. In this case you do:
var
D: TPJMD5Digest;
begin
D := TPJMD5.Calculate('Foo');
ShowMessageFmt('Digest = %s', [string(D)]);
end;
You can’t pass D directly here because, behind the scenes, the second parameter of ShowMessageFmt is converted to an array of TVarRec records, and no suitable implicit cast is defined in TPJMD5Digest. However, a cast to string works because the compiler knows how to convert strings to TVarRec.
Similarly, if you have a console application and want to write a TPJMD5Digest record as a string to the console you need the string cast otherwise WriteLn and its ilk don’t understand the type:
var
D: TPJMD5Digest;
begin
D := TPJMD5.Calculate('Foo');
WriteLn('Digest = ', string(D));
end;
You can also assign a string to a TPJMD5Digest providing the string contains a valid hex representation of a MD5 digest. This uses another Implicit cast operator