Skip to content

Instantly share code, notes, and snippets.

@klang
Created Jul 14, 2015
Embed
What would you like to do?
git hash
import org.apache.commons.codec.digest.DigestUtils;
public class GitSha1 {
public static String githash(String aString){
return DigestUtils.sha1Hex("blob " + aString.length() + "\0" + aString);
}
public static void main(String[] args) {
String theString = "this is the end, there is nothing else";
String expected = "c9ee5714232648dfdaea316c8ae5a2549632e0bb";
String result = githash(theString);
System.out.println(result + " " + (result.equals(expected)?"correct":"incorrect"));
}
}
from hashlib import sha1
def githash(data):
s = sha1()
s.update("blob %u\0" % len(data))
s.update(data)
return s.hexdigest()
# [klang@ergates aws-python]$ python -i sha1.py
# >>> githash("this is the end, there is nothing else")
# 'c9ee5714232648dfdaea316c8ae5a2549632e0bb'
git_sha1 () { printf 'blob %s\0' "$(ls -l "$1" | awk '{print $5;}')" | cat - "$1" | sha1sum | awk '{print $1}'; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment