Skip to content

Instantly share code, notes, and snippets.

@AbrarSyed
Created July 3, 2014 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbrarSyed/b6de9de2a1430f3b0434 to your computer and use it in GitHub Desktop.
Save AbrarSyed/b6de9de2a1430f3b0434 to your computer and use it in GitHub Desktop.
Easy hashing. Generates all the .sha1 and .md5 files for all files recursively.
#!/usr/bin/env groovy
File root;
if (args.length == 0)
root = new File('.').getCanonicalFile()
else
root = new File(args[0])
root.eachFileRecurse(FileType.FILES) { file ->
def path = file.path
if (path.endsWith(".sha1") || path.endsWith(".md5"))
return; // skip hash files
println "hashing $path"
def hashes = hash file
hashes.each { key, val ->
new File(path + "." + key).write(val)
}
}
import java.security.MessageDigest;
import groovy.io.FileType;
def hash(final file) {
MessageDigest digestM = MessageDigest.getInstance("MD5")
MessageDigest digestS = MessageDigest.getInstance("SHA1")
byte[] bytes = file.bytes;
digestM.update bytes
digestS.update bytes
def out = [:]
out["md5"] = new BigInteger(1, digestM.digest()).toString(16).padLeft(32, '0')
out["sha1"] = new BigInteger(1, digestS.digest()).toString(16).padLeft(32, '0')
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment