Code covered by the BSD License  

Highlights from

4.7 | 8 ratings Rate this file 57 Downloads (last 30 days) File Size: 12.5 KB File ID: #25921 Version: 1.1
image thumbnail



Jan Simon (view profile)


23 Nov 2009 (Updated )

Another MD5 calculator as fast C-Mex

| Watch this File

File Information

MD5 is a 128 bit message digest developped by the RSA Data Security Inc. and described in the RFC 1321.

This implementation calculates the MD5 sum for files, strings or byte streams (e.g. UINT8 arrays). The sum can be replied as [1 x 16] double vector, [1 x 32] hexadecimal number or as [1 x 22] base64 number.
For strings either ASCII or Unicode values are considered.

There are some MD5 implementations on the FEX already:
1. Michael Kleder's excellent java method for MD2, MD5, SHA-1/256/384/512 hashes:
The C-Mex CalcMD5 published here is faster: 90% faster for 10kB strings, 30% for 1MB strings, 40% for 10MB strings (see screen shot). In addition CalcMD5 can process files directly without the need of importing them to the memory.
2. Steven Huang's MD5 C-Mex:
This file has some bugs. After fixing them, the speed is similar, because it is based on the same public C-source from the RFC 1321. CalcMD5 handles Unicode strings and byte streams in addition. Huang's MD5 let FOPEN decide, if the input is a file name or a string.
3. Suresh Joel's tool calls an EXE file based on the same C-source:
It works for files only and not for arrays or strings directly. Calling EXE files can be restricted by virus protectors.
4. Some further MD5 tools on the FEX do not reply correct answers.

Summary: CalcMD5 does not add something really new to the FEX. The small advantages are just the speed, the possibility to access files and arrays with the same tool and the different output formats.

Pre-compiled DLLs (Matlab 6) and MEXW32 (Matlab >= 7.6) can be found at:

Now the MEX compiles for 64-bit addressing also, but currently arrays and files are limited to 2.1GB. Full 64 bit support is under construction.


Md5 Checksum and Md5 In Matlab inspired this file.

This file inspired Data Hash.

MATLAB release MATLAB 7.8 (R2009a)
Other requirements Can be compiled with the LCC shipped with Matlab
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
17 Jul 2014 Egor

Egor (view profile)

Thanks, usefull tool!

21 Aug 2013 Eelke Spaak

Is there any progress on the support for large (> 2^31 bytes) variables? This would be very useful to me.

18 Jun 2013 David Hildebrand  
03 Mar 2012 Justin

Justin (view profile)

19 Jul 2011 Philip

Philip (view profile)

Great job, thanks!

30 Dec 2009 Sebastiaan

Glad to hear :)

To make it 'public': Linux users compile with this:
mex CFLAGS="\$CFLAGS -std=c99" CalcMD5.c

29 Dec 2009 Jan Simon

Jan Simon (view profile)

Thanks Sebastiaan! The 64-bit bug is fixed (since 17-Dec-2009). Full 64-bit support is under development.
I like the C99 comment style with //, so your suggestion to add "-std=C99" for the GCC compiler works well.

Comment only
15 Dec 2009 Sebastiaan

Nice functionality, but I have problems using this under Linux, and there is a bug:

First off, it is a C file with C++ style commenting: don't do that - users have to manually disable the -ansi option from their CFLAGS, which is on by default.

The bug is on 64 bit systems, where you assume that unsigned long int is 4 bytes - which is actually 8.

For correct implementation, replace line 104:
typedef unsigned long int UINT32; /* four byte word */
typedef UINT32_T UINT32; /* four byte word */

Using UINT32_T from tmwtypes.h assures 4 bytes on each system.

If you want, I can send you the updated file with C style commenting.

I am looking forward to the version which handles large files and arrays. Maybe I will make one in the future, but I know that LCC is not too happy with long long ints.

07 Dec 2009 Joerg Bretschneider

Great work! Compiling the mex went seamlessly. I used this to get some version control independent from buggy datenum.

17 Dec 2009 1.1

UINT32 has 4 bytes on 64-bit systems now. Thanks to Sebastiaan (34534)!

Contact us