aboutsummaryrefslogtreecommitdiffstats
path: root/common/hash.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2015-10-25 20:30:36 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2015-10-25 20:30:36 +0100
commit9a44cd8ccff143a112436c38bfe5581e74b68f07 (patch)
treef4d2f38cc61647bf159a7c870913e6f6b60828b2 /common/hash.go
parentbe69a505926451bf10450ac68d40265a6f43e150 (diff)
downloadgo-git-9a44cd8ccff143a112436c38bfe5581e74b68f07.tar.gz
formats/packfile: new reader API
Diffstat (limited to 'common/hash.go')
-rw-r--r--common/hash.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/common/hash.go b/common/hash.go
new file mode 100644
index 0000000..83844c7
--- /dev/null
+++ b/common/hash.go
@@ -0,0 +1,35 @@
+package common
+
+import (
+ "crypto/sha1"
+ "encoding/hex"
+ "strconv"
+)
+
+// Hash SHA1 hased content
+type Hash [20]byte
+
+// ComputeHash compute the hash for a given ObjectType and content
+func ComputeHash(t ObjectType, content []byte) Hash {
+ h := t.Bytes()
+ h = append(h, ' ')
+ h = strconv.AppendInt(h, int64(len(content)), 10)
+ h = append(h, 0)
+ h = append(h, content...)
+
+ return Hash(sha1.Sum(h))
+}
+
+// NewHash return a new Hash from a hexadecimal hash representation
+func NewHash(s string) Hash {
+ b, _ := hex.DecodeString(s)
+
+ var h Hash
+ copy(h[:], b)
+
+ return h
+}
+
+func (h Hash) String() string {
+ return hex.EncodeToString(h[:])
+}