Skip over navigation

MD5 Message Digest Unit

Implements the MD5 Message Digest Algorithm

Version 1.1.1
Released 2014-01-11
Platforms Win32 & Win64
Framework RTL
Compatibilty Delphi 2009 & later

Overview

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.

DUnit tests for the unit are supplied in the download. The tests include all those specified in RFC 1321.

So why write this code?

Well, I'd been searching for an up-to-date implementation of the MD5 algorithm that met these criteria:

  1. No dependency on .obj files or DLLs. I wanted pure Pascal code.
  2. 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.
  3. Compiles and works with the Unicode Delphi Compilers.
  4. Object oriented.
  5. 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.

Get The Code

You can download the latest version of the MD5 Message Digest Unit directly from SourceForge as dd-md5-1.1.1.zip.

Download v1.1.1

The MD5 Message Digest Unit source code is hosted in the ddablib/md5 GitHub repository. You can git clone or fork the repository as required.

Installation

The MD5 Message Digest Unit, documentation and test suite are supplied in a zip file. Before installing you need to extract all the files, preserving the directory structure. The following files will be extracted:

  • PJMD5.pas – source code.
  • ReadMe.htm – read-me file.
  • ChangeLog.txt – project change log.
  • Documentation.URL – short-cut to the unit's online documentation.
  • License.txt – the project's license.
  • MPL-2.txt – the Mozilla Public License v2.0.
  • MD5-Notice.txt – MD5 algorithm licensing notice.

In addition to the above files you will find the DUnit tests for PJMD5.pas in the Test sub-directory.

There are three possible ways to use the unit.

  1. The simplest way is to add PJMD5.pas to your projects as you need it.
  2. To make the unit easier to re-use you can either copy it to a folder on your Delphi search path, or add the folder where you extracted the unit to the Delphi Search path. You then simply use the unit as required without needing to add it to your project.
  3. For maximum portability you can add the unit to a Delphi 32 bit or 64 bit Windows package. If you need help doing this see here.

Documentation

The MD5 Message Digest Unit is comprehensively documented online here

The project's change log can be viewed here.

A read-me file (ReadMe.htm) is included in the project download.

Feedback & Queries

If you find any bugs or want to suggest a new feature please report them using the MD5 Message Digest Unit issue tracker.

If you have created a bug fix or have implemented a new feature please open a pull request for it.

Should you have any queries about using the unit please read the documentation. If you can't find an answer in the documentation then post a message in the discussion group.

License

The MD5 Message Digest Unit is open source. It is copyright © 2010-2014 by Peter Johnson. The source code is made available under the terms of the Mozilla Public License v2.0. All relevant trademarks are acknowledged.