aboutsummaryrefslogtreecommitdiffstats
path: root/core/hash.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-02-26 09:14:09 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2016-02-26 09:14:09 +0100
commit1e74b17f05ad27818df39818a0d22107a0b4b424 (patch)
tree3b96f28ce8196f290522dd8983a4965fdbd090f0 /core/hash.go
parent5bc563727ffa798caee3b007c366eb66c3d69caa (diff)
parent397526522d8b81a1485671146acb0ed58a6bfa98 (diff)
downloadgo-git-1e74b17f05ad27818df39818a0d22107a0b4b424.tar.gz
Merge pull request #35 from scjalliance/hasher
Added Hasher for computing hashes of streamed objects
Diffstat (limited to 'core/hash.go')
-rw-r--r--core/hash.go29
1 files changed, 22 insertions, 7 deletions
diff --git a/core/hash.go b/core/hash.go
index 9b30b6e..c50ffb2 100644
--- a/core/hash.go
+++ b/core/hash.go
@@ -3,6 +3,7 @@ package core
import (
"crypto/sha1"
"encoding/hex"
+ "hash"
"strconv"
)
@@ -14,13 +15,9 @@ var ZeroHash Hash
// 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))
+ h := NewHasher(t, int64(len(content)))
+ h.Write(content)
+ return h.Sum()
}
// NewHash return a new Hash from a hexadecimal hash representation
@@ -41,3 +38,21 @@ func (h Hash) IsZero() bool {
func (h Hash) String() string {
return hex.EncodeToString(h[:])
}
+
+type Hasher struct {
+ hash.Hash
+}
+
+func NewHasher(t ObjectType, size int64) Hasher {
+ h := Hasher{sha1.New()}
+ h.Write(t.Bytes())
+ h.Write([]byte(" "))
+ h.Write([]byte(strconv.FormatInt(size, 10)))
+ h.Write([]byte{0})
+ return h
+}
+
+func (h Hasher) Sum() (hash Hash) {
+ copy(hash[:], h.Hash.Sum(nil))
+ return
+}