Skip to content

Instantly share code, notes, and snippets.

@FredericJacobs
Created May 7, 2012 12:20
Show Gist options
  • Save FredericJacobs/2627474 to your computer and use it in GitHub Desktop.
Save FredericJacobs/2627474 to your computer and use it in GitHub Desktop.
Hashing
private byte[] generateHash() {
MessageDigest messageDigestInstance = null;
byte[] hash = null;
try {
messageDigestInstance = MessageDigest.getInstance("SHA-1");
DigestInputStream dis = new DigestInputStream(new DataInputStream(
new FileInputStream(this)), messageDigestInstance);
while (dis.read() != -1)
;
dis.close();
hash = messageDigestInstance.digest();
} catch (FileNotFoundException e) {
System.err
.println("File not found while generating hash for DataFile.");
} catch (NoSuchAlgorithmException e) {
System.err
.println("Algorithm does not exist while generating hash for DataFile.");
} catch (IOException e) {
System.err
.println("I/O Exception while generating hash for DataFile.");
}
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment