I'm using MessageDigest to make the hash of files using SHA 256 as follow:
byte[] hash = new byte[32];
MessageDigest digest = MessageDigest.getInstance("SHA-256");
try (InputStream input = Files.newInputStream( Paths.get(file.getPath()) )) {
byte[] buf = new byte[8192];
int len;
while ( (len=input.read(buf)) > 0 ) {
digest.update(buf, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hash = digest.digest();
The "simplified" idea is: i hash a file, take only the two first bytes, send it two a server; server looks in his DB ob he already have this "shorthash" (i mean, the two bytes). If yes, client isn't allowed to send the file, which will be save in DB with the shorthash.
Problem is: if i give two times the same file, it won't give me the same hash. And i have no idea why.
Thanks to Robert, it seems it was just a printing problem. I was printing it this way and get two weird String beginning with B@:
System.out.println(hash);
By doing it this way i get two int array which are exactly the same:
System.out.println(Arrays.toString(hash))
Now i just have to find out why my DataBase doesn't see they're the same. Since this is probably due to SQL statement, this is no more the subject.