Created
July 3, 2014 23:28
-
-
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.
This file contains hidden or 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
#!/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