Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0


There are a number of posts on the net about SHA1 hashing in groovy but they tend to assume small data sets. This script calculates a SHA1 hash for an arbitrary size argument file using a one MB memory buffer.

Note that the script uses the File.eachByte(int, closure) method which is only available in Groovy v1.7.4 and later. You can naturally replace this construct with some other pattern, but as it fits so beautifully for the purpose, the script makes use of it.


Paste the below into a file "sha1.groovy" and run with a file param, i.e.:

Code Block
titleCalling the script
> groovy sha1.groovy myLargeFile.iso

and now for the code:

Code Block
titleScript Code

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"

Author: Matias Bjarland, Iteego Inc