5

I want to calculate MD5 (or other) file hashes (RFC 1321 conform) within MATLAB using the Java-Security-Implementations. Thus I coded

mddigest=java.security.MessageDigest.getInstance('MD5');
filestream=java.io.FileInputStream(java.io.File(filename));
digestream=java.security.DigestInputStream(filestream,mddigest);
md5hash=reshape(dec2hex(typecast(mddigest.digest,'uint8')),1,[])

and the routine is working fine. Somehow, the result differs from given tools.
Maybe there are problems with the file-encoding? Should't MATLAB solve that internally?
I'd like to reproduce the results, one gets by md5sum (on linux), which equal those from HashCalc (Windows).

3 Answers 3

6

There is two problems:

  1. You don't read the file.
  2. You have to transpose the matrix before reshape it.

This code works:

mddigest   = java.security.MessageDigest.getInstance('MD5'); 
filestream = java.io.FileInputStream(java.io.File(filename)); 
digestream = java.security.DigestInputStream(filestream,mddigest);

while(digestream.read() ~= -1) end

md5hash=reshape(dec2hex(typecast(mddigest.digest(),'uint8'))',1,[]);

/!\ Edit : p.vitzliputzli answered a very much faster solution which should be used instead of this one.

4
  • 1
    Haven't you missed a couple of news? Oct 31, 2012 at 20:57
  • Hi @TobiasRitzau and Stéphane Pinchaux, I'm very sorry, that it took such a long time. Yout code works fine - thanks. Merry christmas. Dec 13, 2013 at 7:55
  • i get Undefined variable "algorithm" or class "algorithm.digest". Is there a prefix before algorithm.digest that i need to add? Or should it be "mddigest.digest" instead of "algorithm.digest"?
    – Eagle
    May 12, 2015 at 5:22
  • @Eagle: Yes, it should be mddigest instead of algorithm. I have edited the answer. Nov 7, 2016 at 15:09
3

Stephane's solution works but is quite slow due to MATLAB's limitation of not being able to supply a JAVA byte[] array to the read method of the DigestInputStream (or any other InputStream).

However, we can adapt Thomas Pornin's solution (discarding the FileInputStream) in order to arrive at:

mddigest   = java.security.MessageDigest.getInstance('MD5'); 

bufsize = 8192;

fid = fopen(filename);

while ~feof(fid)
    [currData,len] = fread(fid, bufsize, '*uint8');       
    if ~isempty(currData)
        mddigest.update(currData, 0, len);
    end
end

fclose(fid);

hash = reshape(dec2hex(typecast(mddigest.digest(),'uint8'))',1,[]);

This solution takes about 0.018s to compute the hash of a 713kB file whereas the other solution takes about 31s.

2

You never read from the DigestInputStream.

This means no bytes will be digested.

You must read the entire file (via the DigestInputStream) and then call digest to get the digest value.

1
  • Oh, thanks Joachim - this is needed. But further, as I noted down, I'd like to build file-hasher conform to RFC1321, thus with a one bit and further zero padding and after that adding 64 Bit length. Is there a way to get that into the stream by hand? Aug 27, 2012 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.