JAVA.FILE_CHECKSUM compute checksum hash of file
Ref: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#getInstance(java.lang.String)
function hash = file_checksum(file, hash_method)
if strcmpi(hash_method, 'sha256')
hash_method = "SHA-256";
end
file_chunk = 10e6;
fid = fopen(file, 'r');
assert(fid > 1, "could not open file %s", file)
try
inst = javaMethod('getInstance', 'java.security.MessageDigest', hash_method);
while ~feof(fid)
[bytes, count] = fread(fid, file_chunk, '*uint8');
if count
inst.update(bytes);
end
end
hash = sprintf('%.2x', typecast(inst.digest, 'uint8'));
catch e
javaException(e)
hash = '';
end
fclose(fid);
end