File Exchange

image thumbnail


version (708 Bytes) by Marcin Konowalczyk
Use a java routine to calculate MD5 hash of a file


Updated 12 Jul 2017

View License

This function attempts to calculate a MD5 checksum of a file. It is returned as a 32 character string - lower case hexadecimal.
This file is a one-file implementation. The code is concise and can also be included in your own functions. The purpose of this is to avoid having to compile c-code into mex files for simple implementations.
MD5 checksums should be used only to check for the file integrity. They are cryptographically broken, and unsuitable for security purposes.

Cite As

Marcin Konowalczyk (2020). JavaMD5 (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (1)


For a 200MB this takes over 6 seconds on my Win7/64, Core2Duo, Matlab 2015b. GetMD5.mex meeds only 0.9 seconds. Although the C implementation is known to be faster, the factor 6.6 is not expected.

If I replace the code for importing the data: "fread(fid)" by "fread(fid, inf, '*uint8')", JavaMD5 needs 2.2 seconds only. Importing the file as DOUBLE uses 8 times as much memory as using UINT8 directly. For a file with several GB this would help to avoid exhausting the RAM completely.

DataHash calls the Java method also, but requires 1.5 seconds only, because the file is processed in chunks of 1MB.

If a file is not existing, a regular ERROR would be better than replying an invalid hash. Note that FOPEN is smart and searchs the Matlab PATH automatically. So better use absolute file names as input.


added error IDs

Replaced "fread(fid)" by "fread(fid, inf, '*uint8') for speed
try/catch now trows an error as opposed to returning an invalid hash

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: GetMD5, DataHash