Last active
January 17, 2017 10:52
-
-
Save 166MMX/e4bc08059bccd2587595 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.DigestInputStream | |
import java.security.MessageDigest | |
import java.util.zip.CRC32 | |
import java.util.zip.CheckedInputStream | |
def calculateHash(String fileName) { | |
MessageDigest md5 = MessageDigest.getInstance('MD5') | |
MessageDigest sha1 = MessageDigest.getInstance('SHA-1') | |
MessageDigest sha256 = MessageDigest.getInstance('SHA-256') | |
MessageDigest sha512 = MessageDigest.getInstance('SHA-512') | |
FileInputStream fis = new FileInputStream(fileName) | |
BufferedInputStream bis = new BufferedInputStream(fis) | |
DigestInputStream md5Dis = new DigestInputStream(bis, md5) | |
DigestInputStream sha1Dis = new DigestInputStream(md5Dis, sha1) | |
DigestInputStream sha256Dis = new DigestInputStream(sha1Dis, sha256) | |
DigestInputStream sha512Dis = new DigestInputStream(sha256Dis, sha512) | |
CheckedInputStream checkIn = new CheckedInputStream(sha512Dis, new CRC32()) | |
while (checkIn.read() != -1); | |
fis.close() | |
println "MD5 " + byteArray2Hex(md5.digest()) | |
println "SHA1 " + byteArray2Hex(sha1.digest()) | |
println "SHA256 " + byteArray2Hex(sha256.digest()) | |
println "SHA512 " + byteArray2Hex(sha512.digest()) | |
println "CRC32 " + checkIn.checksum.value | |
} | |
String byteArray2Hex(byte[] hash) { | |
Formatter formatter = new Formatter() | |
for (byte b : hash) { | |
formatter.format('%02x', b) | |
} | |
return formatter | |
} | |
calculateHash('C:\\Windows\\win.ini') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment