aboutsummaryrefslogtreecommitdiff
path: root/hash.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-01-29 21:17:25 +0100
committerGravatar Runxi Yu2026-01-29 21:18:05 +0100
commit7644a12dd8fbeb0c936848a4bc5cef423a8fc2b7 (patch)
tree75db96d1d24f178be68907577897614cb855d09b /hash.go
parentmurmurhash2: Delete (diff)
signatureNo signature
hash: Make streaming hashes possible
Diffstat (limited to 'hash.go')
-rw-r--r--hash.go16
1 files changed, 16 insertions, 0 deletions
diff --git a/hash.go b/hash.go
index cbaac821..81b269c3 100644
--- a/hash.go
+++ b/hash.go
@@ -4,6 +4,7 @@ import (
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
+ "hash"
)
// maxHashSize MUST be >= the largest supported algorithm size.
@@ -22,6 +23,7 @@ type hashAlgorithmDetails struct {
name string
size int
sum func([]byte) Hash
+ new func() hash.Hash
}
var hashAlgorithmTable = [...]hashAlgorithmDetails{
@@ -36,6 +38,9 @@ var hashAlgorithmTable = [...]hashAlgorithmDetails{
h.algo = hashAlgoSHA1
return h
},
+ new: func() hash.Hash {
+ return sha1.New()
+ },
},
hashAlgoSHA256: {
name: "sha256",
@@ -47,6 +52,9 @@ var hashAlgorithmTable = [...]hashAlgorithmDetails{
h.algo = hashAlgoSHA256
return h
},
+ new: func() hash.Hash {
+ return sha256.New()
+ },
},
}
@@ -76,6 +84,14 @@ func (algo hashAlgorithm) Sum(data []byte) Hash {
return algo.info().sum(data)
}
+func (algo hashAlgorithm) New() (hash.Hash, error) {
+ newFn := algo.info().new
+ if newFn == nil {
+ return nil, ErrInvalidObject
+ }
+ return newFn(), nil
+}
+
// Hash represents a Git object ID.
type Hash struct {
algo hashAlgorithm