2

I'm trying to calculate a hash with Matlab to replicate a function made by a third party (not in matlab environment). The output of the original is the same as that calculated via java, the problem is that it's too slow for my purposes and so I was looking for a faster method.

Using the DataHash function (https://viewer.mathworks.com/?viewer=plain_code&url=https%3A%2F%2Fin.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2Ff6707982-0633-496b-bec1-d7ed0fd32b86%2F8440fb29-dcff-8196-0d6b-b23f6b6daf38%2Ffiles%2Fcore%2Ffunctions%2Futilities%2FDataHash.m&embed=web) surely the count is much faster but the result is different.

I wanted to know if there is a configuration to make the results equal (I'm not an expert), or if you know of other methods to calculate the hash faster.

Thank you very much

% Example data
data = 'Hello, world!';

% Calculate SHA256 hash in Matlab
matlabHash = DataHash(data, 'SHA-256', 'hex');

% Calculate SHA256 hash in Java
javaHash = java.security.MessageDigest.getInstance('SHA-256');
javaHash.update(uint8(data));
javaHash = sprintf('%02x', typecast(javaHash.digest, 'uint8'));

% Compare the results
if strcmp(javaHash, matlabHash)
    disp('The two hashes are identical.');
else
    disp('The two hashes are different.');
    disp(['SHA256 hash in Java: ', javaHash]);
    disp(['SHA256 hash in MATLAB: ', matlabHash]);
end

The two hashes are different.
SHA256 hash in Java: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
SHA256 hash in MATLAB: 48caecb9db2255f4a10843d2bd36aa928c342566bbb77aee3df8100a30b7f7b4

RE-EDIT: I used this and it works:

sha256hasher = System.Security.Cryptography.SHA256Managed;
sha256 = uint8(sha256hasher.ComputeHash(fileBytes));
hash = reshape(dec2hex(sha256)', 1, []);
5
  • What are your speed requirements? With a test string 1 million characters long your javaHash approach takes 4ms on my machine, is that the bottleneck in your code? Commented Apr 29, 2024 at 8:01
  • let's say with the javaHash it takes 7/8 minutes to process my files, the DataHash less than 1 second. Not that I necessarily want to get to 1 second, but I would like to stay under 1 minute. Commented Apr 29, 2024 at 9:52
  • Can you give an example which shows that dramatically slow behaviour? Are you hashing a few enormous strings or many large strings? It will be hard to benchmark any improvement using the hello-world example as above, although I appreciate it is technically a minimal reproducible example Commented Apr 29, 2024 at 10:23
  • ok, I'll edit the original post because the code seems to be too long Commented Apr 29, 2024 at 11:44
  • 2
    There is this comment from the code at your link: MATLAB CHARs have 16 bits! In consequence the string 'hello' is treated as UINT16('hello') for the binary input method. Perhaps if you supplied DataHash with uint8(data) instead of data it would return the same result. Commented Apr 29, 2024 at 12:03

1 Answer 1

0

They are different because DataHash takes the length and the type of the message into consideration. so different message of same contents and different types give different outputs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.