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, []);
javaHashapproach takes 4ms on my machine, is that the bottleneck in your code?'hello'is treated asUINT16('hello')for the binary input method. Perhaps if you supplied DataHash withuint8(data)instead ofdatait would return the same result.