Skip to content

Instantly share code, notes, and snippets.

@azenla
Created May 17, 2013 00:33
Show Gist options
  • Save azenla/5596183 to your computer and use it in GitHub Desktop.
Save azenla/5596183 to your computer and use it in GitHub Desktop.
Groovy Script to calculate SHA1 Sums for HUGE files
import java.security.MessageDigest
int KB = 1024
int MB = 1024*KB
File f = new File(args[0])
if (!f.exists() || !f.isFile()) {
println "Invalid file $f provided"
println "Usage: groovy sha1.groovy <file_to_hash>"
}
def messageDigest = MessageDigest.getInstance("SHA1")
long start = System.currentTimeMillis()
f.eachByte(MB) { byte[] buf, int bytesRead ->
messageDigest.update(buf, 0, bytesRead);
}
def sha1Hex = new BigInteger(1, messageDigest.digest()).toString(16).padLeft( 40, '0' )
long delta = System.currentTimeMillis()-start
println "$sha1Hex took $delta ms to calculate"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment