© 2009 W.Ehrhardt Last update Oct. 12, 2009
Home CRC/Hash Crypto Misc. Info Links Deutsch

CRC / HASH / HMAC

Here you find a few Pascal / Delphi source and binary files related to CRC, hash, and HMAC calculations. All files can be used and distributed under this license, the original template of the zlib/libpng license can be found here: zlib-license.

Before downloading any software from this site please read this legal notice.

The basic routines in crc_hash_2009-10-12.zip can be compiled with most current Pascal (TP 5/5.5/6, BP 7, VP 2.1, FPC 1.0/2.0/2.2) and Delphi versions (tested with V1 up to V7/9/10/12).


Last changes: 
Since Feb. 2006 there is a new Hash/HMAC architecture: Hash descriptor records allow a simple and uniform HMAC implementation for all hash algorithms; the key derivation functions can use all supported hash algorithms. A separate short introduction gives some more information about the Hash/HMAC units and procedures.

Since May 2008 the cryptographic hash and HMAC routines support messages with arbitrary bit lengths.


The basic routines were slightly improved in the previous versions, but optimizing seems to be black magic. The cycles/times are heavily dependent on CPU, cache, compiler, code position, etc. For example: if the SHA256 loop is unrolled, the calculation slows down for about 40% on one machine (1.8GHz P4, D6, Win98), but is about 15% faster on another (AMD 2600+, D5, Win98). In the archive sha256unroll.zip you can find some snippets related to SHA256 loop unrolling.

With the test program T_SpeedA and the high resolution timer from hrtimer you can measure the CPU cycles per byte (Cyc/B) and the processing rate in MB/s (note that the CPU frequency is determined dynamically). Here are the values for Delphi/FPC on Win98 with Pentium 4 / 1.7 GHz using a blocksize of 50000 bytes (Std: standard routines with BASM, PP: Pure Pascal with inline for D10 and FPC2.2.4 -O3):

D3/Std D3/Std D6/Std D6/PP D10/PP FPC/PP
Name MB/s Cyc/B Cyc/B Cyc/B Cyc/B Cyc/B
CRC16 201.01 8.4 8.5 33.2 26.7 43.8
CRC24 179.33 9.5 9.4 29.9 26.3 34.1
CRC32 278.82 6.1 6.1 17.9 19.0 24.7
FCRC32 389.11 4.4 4.4 19.3 17.9 18.6
Adler32 367.39 4.6 4.4 4.5 3.6 8.1
CRC64 93.47 18.1 18.3 93.2 85.1 61.0
eDonkey 208.72 8.1 8.1 8.1 8.1 19.7
MD4 209.55 8.1 8.1 8.1 8.1 19.8
MD5 151.21 11.2 11.2 11.2 11.2 11.0
RMD160 53.30 31.8 31.8 31.9 30.8 98.6
SHA1 50.10 33.8 35.6 38.4 35.9 52.3
SHA224 28.01 60.5 54.3 52.6 50.4 62.2
SHA256 28.20 60.1 56.5 53.5 50.9 63.0
SHA384 9.78 173.3 205.9 206.5 206.1 194.6
SHA512 9.78 173.3 205.9 206.3 207.0 194.6
Whirlpool 17.13 98.9 129.8 129.8 99.4 98.4
BMW224 28.56 59.3 59.4 58.8 30.9 42.9
BMW256 28.50 59.5 59.6 58.7 32.0 44.2

MD4, eDonkey/eMule: For files/messages with a multiple of 9728000 bytes the eDonkey and eMule hashes are different; the ed2k unit always calculates both digests. The demo programs and the FAR plugin display both values if they are different.

Int64 support for SHA384/512:  Unfortunately there are conflicting processor specific results: on a P4 / 1.8GHz the speed decreases to 83% of the longint speed (Cyc/B increase from 174 to 209). For a Celeron 500MHz the speed increases more than 30%, the Cyc/B decrease from 146 (longint) to 111 (Int64). In the source Int64 is default for D4+ and FPC (conditional define UseInt64 in SHA512.PAS)

BASM16 table alignment: Because some BASM16 implementations use 32 bit access to 32 bit tables, these tables should be dword aligned for optimal speed. But the 16 bit compilers support byte or word alignment only! Therefore the defines from the align.inc include file allow to generate dummy words, which align the tables to 32 bit boundaries. This feature is implemented for CRC24 ... CRC64; if more than one of these units are used, it may be necessary to iterate the alignment procedure.

Rocksoft™ Model CRC Algorithm: The crcmodel unit is a Pascal implementation of Ross William's parameterized model CRC algorithm described in A Painless Guide to CRC Error Detection Algorithms (local HTML version). Most of the usual CRC algorithms with polynomials up to degree 32 can be modeled by this unit. The crcm_cat unit has predefined parameter records for more than 40 CRC algorithms, most of them adapted from Greg Cook's Catalogue of Parameterised CRC Algorithms, more references are listed in the unit header. The GUI demo programs tcrc16/tcrc32 interactively calculate and display the results from all crcm_cat CRC16/CRC32 algorithms for hex and string input, SRP16 searches CRC16 Rocksoft parameters for given data; the archive chksum_bin_2009-10-12.zip includes the EXE files.

Blue Midnight Wish: An experimental version of Blue Midnight Wish hash function is included in the archive bmw_2009-10-12.zip. BMW is one of the fastest algorithms of the second round of the SHA3 contest. Only the 224 and 256 bit versions are implemented, because the 384/512 bit versions have internal state vectors with lengths greater than the current maximum length in the hash unit. The algorithms do not have symbols in the hash unit and therefore the FindHash_by_ functions are not applicable, BMW224/256 can be used directly or via descriptor pointers from the units. Also included in the archive are the HMAC functions (unit HMACBMW).


Pure Pascal versions: The archive ppch_2009-07-25.zip contains pure Pascal versions of the basic routines without BASM. The units in the archive are replacements for the standard source. The main purpose is to supply sources for more portable code (e.g. for Linux/ARM). Consequently the code layout is for FPC without asm, but it can be compiled with Delphi 4+. The new std.inc defines {$asmmode intel} if compiled with FPC for CPUI386; if the compiler complains about asm code, this definition should be deleted.

PPCH is based on the standard 32 bit code, some time critical functions are inline (available for FPC2 and D9+). The new CRC routines are significantly slower than the standard sources (see table), but the Hash function speeds are not so uniform.

Special thanks goes to Nicola Lugato who asked for the pure Pascal units and tested the first versions on his ARM/Linux machine.

Please note: The PPCH source still assumes little-endian target platforms. Serious requesters for big-endian should send a mail, and should be willing to do some tests and bug reports.


CRC/Hash demo programs

Last changes:  SRP16 demo program for searching CRC16 Rocksoft parameters

The archive chksum_src_2009-10-12.zip contains Pascal source files demonstrating the use of the CRC and hash units. The command line demo tool CCH.EXE can be compiled with all current Pascal and Delphi versions, with Virtual Pascal and BP 7 you can build a dual OS program running as a 16 bit EXE under DOS and as a 32 bit console application supporting long file names under Win32 (use make_cch.bat). GCH.EXE is a simple Win32 GUI demo application for Delphi 2..7/9/10/12  (Screenshot). Input files can be given as command line parameters.

The GUI demo programs tcrc16/tcrc32 interactively calculate and display the results from all crcm_cat CRC16/CRC32 algorithms for hex and string input (Screenshot).

The GUI program SRP16 searches CRC16 Rocksoft parameters for given data / CRC sets with many options. The core routine processes Poly / Init ranges with additional combinations of refin, refout, and xorout values.

Compiled exe files within an SFX installer are in chksum_bin_2009-10-12.zip, for recompilation you need the sources from crc_hash_2009-10-12.zip.


FAR manager plugin

The basic routines are used in the CRC/Hash plugin (Screenshot) for the popular FAR manager:

Last changes:  Version 1.17 from HashCRC117.zip fixes a SHA512/384 bug for file sizes above 512MB.

Special thanks goes to Dmitry Yerokhin who contributed Russian translations of the .hlp and .lng files.

The source code for the plugin can be downloaded here: HashCRC117_Src.zip. To rebuild the DLL file, Virtual Pascal 2.1 or Delphi 2+ is required, as well as the source code of the basic CRC/hash routines (plugins.pas from the FAR manager distribution is included).

History: Version 1.2 splits the SHA256 output to avoid truncation, is compiled with Virtual Pascal, and the calculation can be cancelled with Esc. Version 1.3 comes with an English .lng file and a new Delphi Esc check. New in version 1.4 are CRC64 and Adler32. Plugin version 1.6 has SHA224, SHA384, and SHA512 support and displays only the calculated values (no N/A for unchecked functions), there is an average speedup of about 40% compared to V1.5.

Version 1.7 has configurable HEX/Base64 and upper/lower case HEX display, it can be compiled without errors with D4..D7/9. The base routines are more than 20% faster than V1.6 (improved code and built with Delphi3). Values are normally displayed LSB first, CRC16, CRC32, Adler32 are shown as MSB HEX strings (indicator M in separator line, L otherwise) if the config option Strict LSB is not checked. V1.7.1 is the release with the SHA512 bugfixes.

Version 1.8 has two additional buttons, the results can be written to a file (name of the processed file with an appended extension .chf) or copied to the clipboard.

Version 1.9 supports multi file mode: If the plugin is invoked with several files selected in the active panel the configured CRCs and hash digests are calculated for all selected files and the results are written to a text file (in a format similar to that of CCH). The file name can be edited in an input box, a new config option controls whether I/O error will be written to the result file.

Version 1.10 from supports the V3.0 Whirlpool hash and uses the numbers of lines of the FAR console to prevent the output from overflowing the dialog window. Vers. 1.11 uses the new hash architecture and supports RIPEMD-160, V1.12 adds support for the CRC24 used in (Open)PGP and GnuPG.

Version 1.13 results from a cooperation with Giovanni Drusian. It introduces a special MD5 processing for Portable Executable (PE) files, that is useful for detecting and classifying malware. A new configuration option controls the writing of the complete file paths to the results files or clipboard. V1.14 fixed the Whirlpool bug for file sizes above 512MB, V1.15 contains a fix to avoid the Delphi eof/4GB bug. V1.16 supports the MD4 and eDonkey/eMule hash algorithms and has a new configuration dialog layout.

Old source code versions (V1.1 .. V1.16) are available in this solid RAR archive: HashCRC_OldSource.zip

Home CRC/Hash Crypto Misc. Links Deutsch