This unit contains an object-oriented implementation of the MD5 message digest algorithm.
A Delphi class named TPJMD5 provides overloaded methods for calculating the MD5 of:
-
Files.
-
Byte arrays.
-
Strings: Unicode, ANSI and short strings are all supported.
-
Streams.
-
Untyped data buffers (useful for getting the MD5 of ordinal values).
Data types can be mixed and matched. Class methods provide quick shortcuts to taking the MD5 of a single file or single data item without having to explicitly create and free a TPJMD5 instance.
There is also an "advanced" record, TPJMD5Digest, that encapsulutes a MD5 digest. Operator overloads for assigning digests to and from strings and byte arrays are provided. The digest can also be read in long words chunks.
The code was developed from scratch based on the algorithm presented in RFC 1321. Portions of the code are translated from the reference C code supplied in the RFC. However the code is not a literal translation into Pascal from the reference C code.
The algorithm and reference code are copyright © 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
Testing
DUnit tests for the unit are supplied in the download. The tests include all those specified in RFC 1321.
Rationale
So why write this code?
Well, I'd been searching for an up-to-date implementation of the MD5 algorithm that met these criteria:
-
No dependency on
.obj
files or DLLs. I wanted pure Pascal code.
-
Open source with a license compatible with the Mozilla Public License because I want to use it in an MPLd application of mine, so GPL-only code was ruled out.
-
Compiles and works with the Unicode Delphi Compilers.
-
Object oriented.
-
Easy to get an MD5 digest from all sorts of data without messing about converting it and playing around with contexts etc.
I couldn't find anything that suited, so it seemed easier to start from scratch. After a couple of false starts, this unit is the result.