Created
August 3, 2009 14:43
-
-
Save dima767/160593 to your computer and use it in GitHub Desktop.
This file contains 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
// Just playing with what 'git hash-object' impl. (for git blob types i.e. 'blob size\0contents') would look like in Groovy. | |
// Note: this is not a 'full-blown' implementation of sha1 functionality available in git | |
#!/usr/bin/env groovy | |
import java.security.MessageDigest | |
def sha1(byte[] buffer) { | |
if (!buffer) { | |
return null | |
} | |
MessageDigest.getInstance("SHA1").digest(buffer) | |
} | |
def sha1ToHex(byte[] sha1) { | |
def HEX_DIGITS = "0123456789abcdef" | |
if (!sha1) { | |
return null | |
} | |
def result = new StringBuffer() | |
sha1.each() { | |
result << HEX_DIGITS[(it & 0xF0) >> 4] | |
result << HEX_DIGITS[(it & 0x0F)] | |
} | |
result.toString() | |
} | |
def usage() { | |
println("usage: please provide a name for an existing, non-empty file") | |
System.exit(1) | |
} | |
if(args.size() < 1) { | |
usage() | |
} | |
f = new File(args[0]) | |
if(f.size() == 0) { | |
usage() | |
} | |
buf = "blob ${f.size()}\0".getBytes() | |
out = new ByteArrayOutputStream() | |
out << buf << f.readBytes() | |
println sha1ToHex(sha1(out.toByteArray())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment