Java: Calcular el hash SHA-256 de un archivo grande de manera eficiente
Necesito calcular un hash SHA-256 de un archivo grande (o parte de él). Mi implementación funciona bien, pero es mucho más lenta que el cálculo de CryptoPP de C ++ (25 min. Frente a 10 min para un archivo de ~ 30 GB). Lo que necesito es un tiempo de ejecución similar en C ++ y Java, por lo que los hashes están listos casi al mismo tiempo. También probé la implementación de Bouncy Castle, pero me dio el mismo resultado. Así es como calculo el hash:
int buff = 16384;
try {
RandomAccessFile file = new RandomAccessFile("T:\\someLargeFile.m2v", "r");
long startTime = System.nanoTime();
MessageDigest hashSum = MessageDigest.getInstance("SHA-256");
byte[] buffer = new byte[buff];
byte[] partialHash = null;
long read = 0;
// calculate the hash of the hole file for the test
long offset = file.length();
int unitsize;
while (read < offset) {
unitsize = (int) (((offset - read) >= buff) ? buff : (offset - read));
file.read(buffer, 0, unitsize);
hashSum.update(buffer, 0, unitsize);
read += unitsize;
}
file.close();
partialHash = new byte[hashSum.getDigestLength()];
partialHash = hashSum.digest();
long endTime = System.nanoTime();
System.out.println(endTime - startTime);
} catch (FileNotFoundException e) {
e.printStackTrace();
}