|
23 | 23 |
|
24 | 24 | file_chunk = 10e6; % arbitrary (bytes) didn't seem to be very sensitive for speed |
25 | 25 |
|
26 | | -inst = javaMethod("getInstance", "java.security.MessageDigest", method); |
27 | | - |
28 | 26 | fid = fopen(file, 'r'); |
29 | 27 | assert(fid > 1, "could not open file %s", file) |
30 | 28 |
|
31 | | -while ~feof(fid) |
32 | | - % https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#update(byte) |
33 | | - inst.update(fread(fid, file_chunk, '*uint8')); |
| 29 | + |
| 30 | +if stdlib.has_dotnet() |
| 31 | + inst = System.Security.Cryptography.HashAlgorithm.Create(method); |
| 32 | + while ~feof(fid) |
| 33 | + % https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.hashalgorithm.computehash |
| 34 | + inst.ComputeHash(fread(fid, file_chunk, '*uint8')); |
| 35 | + end |
| 36 | +elseif stdlib.has_java() |
| 37 | + inst = javaMethod("getInstance", "java.security.MessageDigest", method); |
| 38 | + while ~feof(fid) |
| 39 | + % https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#update(byte) |
| 40 | + inst.update(fread(fid, file_chunk, '*uint8')); |
| 41 | + end |
| 42 | +else |
| 43 | + error('no supported hash method found, please install .NET or Java') |
34 | 44 | end |
| 45 | + |
35 | 46 | fclose(fid); |
36 | 47 |
|
37 | | -% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#digest() |
38 | | -hash = typecast(inst.digest, 'uint8'); |
| 48 | +if stdlib.has_dotnet() |
| 49 | + h = uint8(inst.Hash); |
| 50 | +elseif stdlib.has_java() |
| 51 | + % https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#digest() |
| 52 | + h = typecast(inst.digest, 'uint8'); |
| 53 | +end |
39 | 54 |
|
40 | | -hash = sprintf('%.2x', hash); |
| 55 | +hash = sprintf('%.2x', h); |
41 | 56 |
|
42 | 57 | end |
43 | 58 |
|
|
0 commit comments