aboutsummaryrefslogtreecommitdiffstats
path: root/core/hash.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/hash.go')
-rw-r--r--core/hash.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/core/hash.go b/core/hash.go
new file mode 100644
index 0000000..0b6f274
--- /dev/null
+++ b/core/hash.go
@@ -0,0 +1,40 @@
+package core
+
+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) IsZero() bool {
+ var empty Hash
+ return h == empty
+}
+
+func (h Hash) String() string {
+ return hex.EncodeToString(h[:])
+}