Code covered by the BSD License  

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.8 | 9 ratings Rate this file 52 Downloads (last 30 days) File Size: 21.3 KB File ID: #25921 Version: 2.0
image thumbnail



Jan Simon (view profile)


23 Nov 2009 (Updated )

Get MD5 of files or arrays as fast C-Mex

| Watch this File

File Information

MD5 hash of files, strings and arrays (RFC 1321)
Hash = GetMD5(Data, Mode, Format)
  Data: File name or array.
  Mode: String to declare the type of the 1st input.
            'File': Data is a file name as string.
            '8Bit': If Data is a CHAR array, only the 8 bit ASCII part is used.
            'Binary': The MD5 sum is obtained for the contents of Data.
                    This works for numerical, CHAR and LOGICAL arrays.
            'Array': Include the class and dimensions of Data in the MD5
                    sum. This can be applied for (nested) structs, cells
                    and sparse arrays also.
  Format: String, format of the output: hex, HEX, double, uint8, base64
  Hash: A 128 bit number is replied in the specified format.
This function is at least 2 times faster than the corresponding Java method.
For shorter arrays this C-Mex implementation is much faster, see the output
of the included unit test function.
The function DataHash can reply SHA hashes also, but it is remarkably
slower due to the overhead of calling Java. For nested structs this C-Mex
can be 100 times faster. See:

The C-function must be compiled before using: Call the M-function without
inputs. If you do not have a C-compiler, pre-compiled files can be


Md5 In Matlab inspired this file.

This file inspired Java Md5 and Data Hash.

MATLAB release MATLAB 8.6 (R2015b)
MATLAB Search Path
Other requirements C-compiler
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
09 Apr 2016 Jonas Zimmermann

Compilation on MacOS X 10.11 with clang fails because of this line: #define __STDC_WANT_LIB_EXT1__
Removing it makes it compile.

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)!

29 Feb 2016 2.0

Improved speed, the class and size of arrays can be considered, cell and struct arrays accepted. Now arrays and files > 2.1GB are processed.

04 Jun 2016 2.0

Fixed problem with compiling under MacOS X.

Contact us