Skip to content

Instantly share code, notes, and snippets.

@klang
Created July 14, 2015 09:37
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 klang/33289b7f9ea0bc628f3c to your computer and use it in GitHub Desktop.
Save klang/33289b7f9ea0bc628f3c to your computer and use it in GitHub Desktop.
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