This repository was archived by the owner on Sep 11, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +29
-7
lines changed Expand file tree Collapse file tree 2 files changed +29
-7
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package core
33import (
44 "crypto/sha1"
55 "encoding/hex"
6+ "hash"
67 "strconv"
78)
89
@@ -14,13 +15,9 @@ var ZeroHash Hash
1415
1516// ComputeHash compute the hash for a given ObjectType and content
1617func ComputeHash (t ObjectType , content []byte ) Hash {
17- h := t .Bytes ()
18- h = append (h , ' ' )
19- h = strconv .AppendInt (h , int64 (len (content )), 10 )
20- h = append (h , 0 )
21- h = append (h , content ... )
22-
23- return Hash (sha1 .Sum (h ))
18+ h := NewHasher (t , int64 (len (content )))
19+ h .Write (content )
20+ return h .Sum ()
2421}
2522
2623// NewHash return a new Hash from a hexadecimal hash representation
@@ -41,3 +38,21 @@ func (h Hash) IsZero() bool {
4138func (h Hash ) String () string {
4239 return hex .EncodeToString (h [:])
4340}
41+
42+ type Hasher struct {
43+ hash.Hash
44+ }
45+
46+ func NewHasher (t ObjectType , size int64 ) Hasher {
47+ h := Hasher {sha1 .New ()}
48+ h .Write (t .Bytes ())
49+ h .Write ([]byte (" " ))
50+ h .Write ([]byte (strconv .FormatInt (size , 10 )))
51+ h .Write ([]byte {0 })
52+ return h
53+ }
54+
55+ func (h Hasher ) Sum () (hash Hash ) {
56+ copy (hash [:], h .Hash .Sum (nil ))
57+ return
58+ }
Original file line number Diff line number Diff line change @@ -33,3 +33,10 @@ func (s *HashSuite) TestIsZero(c *C) {
3333 hash = NewHash ("8ab686eafeb1f44702738c8b0f24f2567c36da6d" )
3434 c .Assert (hash .IsZero (), Equals , false )
3535}
36+
37+ func (s * HashSuite ) TestNewHasher (c * C ) {
38+ content := "hasher test sample"
39+ hasher := NewHasher (BlobObject , int64 (len (content )))
40+ hasher .Write ([]byte (content ))
41+ c .Assert (hasher .Sum ().String (), Equals , "dc42c3cc80028d0ec61f0a6b24cadd1c195c4dfc" )
42+ }
You can’t perform that action at this time.
0 commit comments