Pesquisei vários algoritmos de hash, e encontrei alguns de exemplo no SOen, mas eles estão retornando hashes diferentes para o mesmo arquivo:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class ObterHash {
private static String algoritmo = "SHA-256";
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
File arq = new File("C:\\img.jpg");
System.out.println(toHex(gerarHash1(arq)));
System.out.println(toHex(gerarHash2(arq)));
System.out.println(toHex(gerarHash3(arq)));
}
// Adaptado de: https://stackoverflow.com/a/19304310/7711016
public static byte[] gerarHash1(File arq) throws NoSuchAlgorithmException, IOException {
DigestInputStream shaStream = new DigestInputStream(new FileInputStream(arq),
MessageDigest.getInstance(algoritmo));
// VERY IMPORTANT: read from final stream since it's FilterInputStream
byte[] shaDigest = shaStream.getMessageDigest().digest();
shaStream.close();
return shaDigest;
}
// Adaptado de: https://stackoverflow.com/a/26231444/7711016
public static byte[] gerarHash2(File arq) throws IOException, NoSuchAlgorithmException {
byte[] b = Files.readAllBytes(arq.toPath());
byte[] hash = MessageDigest.getInstance(algoritmo).digest(b);
return hash;
}
// Adaptado de: https://stackoverflow.com/a/304275/7711016
public static byte[] gerarHash3(File arq) throws NoSuchAlgorithmException, IOException {
InputStream fis = new FileInputStream(arq);
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance(algoritmo);
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
return complete.digest();
}
private static String toHex(byte[] bytes) {
StringBuilder ret = new StringBuilder();
for (int i = 0; i < bytes.length; ++i) {
ret.append(String.format("%02X", (bytes[i] & 0xFF)));
}
return ret.toString();
}
}
Quando executado, tive essa saída (um hashcode em cada linha):
E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 010F60D2927A35D0235490136EF9F4953B7EE453073794BCAF153D20A64544EA 010F60D2927A35D0235490136EF9F4953B7EE453073794BCAF153D20A64544EA
Veja que o hash gerado por gerarHash2()
e gerarHash3()
são iguais entre si, mas diferentes do gerarHash1()
. Por quê? É o algoritmo de gerarHash1()
que está errado? Se sim, qual é o erro nele?