diff options
| author | 2026-01-29 21:17:25 +0100 | |
|---|---|---|
| committer | 2026-01-29 21:18:05 +0100 | |
| commit | 7644a12dd8fbeb0c936848a4bc5cef423a8fc2b7 (patch) | |
| tree | 75db96d1d24f178be68907577897614cb855d09b /hash.go | |
| parent | murmurhash2: Delete (diff) | |
| signature | No signature | |
hash: Make streaming hashes possible
Diffstat (limited to 'hash.go')
| -rw-r--r-- | hash.go | 16 |
1 files changed, 16 insertions, 0 deletions
@@ -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 |
