Skip to content

Instantly share code, notes, and snippets.

@nobeans
Created March 4, 2011 00:37
Show Gist options
  • Save nobeans/853924 to your computer and use it in GitHub Desktop.
Save nobeans/853924 to your computer and use it in GitHub Desktop.
import java.security.MessageDigest
println "-"*40
println "MessageDigest:"
File.metaClass.getMd5 = {
MessageDigest.getInstance("md5").
digest(delegate.readBytes()).
collect{ String.format("%02x", it) }.
join()
}
File.metaClass.getSha1 = {
MessageDigest.getInstance("sha1").
digest(delegate.readBytes()).
collect{ String.format("%02x", it) }.
join()
}
println new File(args[0]).md5
println new File(args[0]).sha1
println "-"*40
println "AntBuilder#checksum:"
def getMd5(File file) {
new AntBuilder().with{
checksum(file: file, algorithm: 'md5', property: 'result')
it.project.properties.result
}
}
def getSha1(File file) {
new AntBuilder().with{
checksum(file: file, algorithm: 'sha1', property: 'result')
it.project.properties.result
}
}
println getMd5(new File(args[0]))
println getSha1(new File(args[0]))
@nobeans
Copy link
Author

nobeans commented Mar 4, 2011

おお。せっかくGroovyベースならGrapeを使えば予備動作なしにJRubyも実行できますね!

@Grab("org.jruby:jruby-complete:1.5.6")
import org.jruby.embed.*

println "-"*40
println "JRuby:"
container = new ScriptingContainer(LocalContextScope.SINGLETHREAD, LocalVariableBehavior.PERSISTENT)
container.runScriptlet("require 'digest'")
File.metaClass.getMd5JRuby = {
    container.put("bytes", delegate.readBytes())
    container.runScriptlet("Digest::MD5.hexdigest(String.from_java_bytes(bytes)).to_java")
}
File.metaClass.getSha1JRuby = {
    container.put("bytes", delegate.readBytes())
    container.runScriptlet("Digest::SHA1.hexdigest(String.from_java_bytes(bytes)).to_java")
}
println new File(args[0]).md5JRuby
println new File(args[0]).sha1JRuby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment