Code covered by the BSD License  

Highlights from
CalcMD5

4.66667

4.7 | 4 ratings Rate this file 46 Downloads (last 30 days) File Size: 12.47 KB File ID: #25921
image thumbnail

CalcMD5

by Jan Simon

 

23 Nov 2009 (Updated 17 Dec 2009)

Another MD5 calculator as fast C-Mex

| Watch this File

File Information
Description

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:
  http://www.mathworks.com/matlabcentral/fileexchange/8944
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:
  http://www.mathworks.com/matlabcentral/fileexchange/7919
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:
  http://www.mathworks.com/matlabcentral/fileexchange/3432
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:
http://n-simon.de/mex

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.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
MD5 Checksum, MD5 in MATLAB
This submission has inspired the following:
DataHash

MATLAB release MATLAB 7.8 (R2009a)
Other requirements Can be compiled with the LCC shipped with Matlab
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
07 Dec 2009 Joerg Bretschneider

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

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 */
with
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.

29 Dec 2009 Jan Simon

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.

30 Dec 2009 Sebastiaan

Glad to hear :)

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

19 Jul 2011 Philip

Great job, thanks!

Please login to add a comment or rating.
Updates
17 Dec 2009

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

Tag Activity for this File
Tag Applied By Date/Time
md5 Jan Simon 23 Nov 2009 09:55:40
checksum Jan Simon 23 Nov 2009 09:55:40
cmex Jan Simon 23 Nov 2009 09:55:40
digest Jan Simon 23 Nov 2009 09:55:40

Contact us at files@mathworks.com