From 7644a12dd8fbeb0c936848a4bc5cef423a8fc2b7 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 29 Jan 2026 21:17:25 +0100 Subject: hash: Make streaming hashes possible --- hash.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'hash.go') 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 -- cgit v1.3.1-10-gc9f91