I have searched for several hash algorithms, and found some example in SOen, but they are returning different hashes to the same file:
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();
}
}
When run, I had this output (a hashcode on each line):
E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 010F60D2927A35D0235490136EF9F4953B7EE453073794BCAF153D20A64544EA 010F60D2927A35D0235490136EF9F4953B7EE453073794BCAF153D20A64544EA
See that the hash generated by gerarHash2()
and gerarHash3()
are equal to each other but different from gerarHash1()
. Because? Is the gerarHash1()
algorithm wrong? If so, what is the error in it?