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.