aboutsummaryrefslogtreecommitdiff
path: root/hash.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-01-19 01:03:23 +0800
committerGravatar Runxi Yu2026-01-19 01:03:23 +0800
commit32f48e18896061529f038eaf527f2eec0490f5df (patch)
treeb6f09abd46b297b2e929e29d98abefb02b43b6ff /hash.go
parentgo.mod: Use the codeberg import path (diff)
signatureNo signature
hash: Document maxHashSize properly
Diffstat (limited to 'hash.go')
-rw-r--r--hash.go40
1 files changed, 21 insertions, 19 deletions
diff --git a/hash.go b/hash.go
index 1a0053c2..df39ac30 100644
--- a/hash.go
+++ b/hash.go
@@ -6,11 +6,31 @@ import (
"encoding/hex"
)
-const maxHashSize = 32
+// maxHashSize MUST be equal to (or larger than) the size of the
+// largest hash supported in hashFuncs.
+const maxHashSize = sha256.Size
// hashAlgorithm identifies the hash algorithm used for Git object IDs.
type hashAlgorithm uint8
+// hashFuncs maps hash algorithm to hash function.
+var hashFuncs = map[hashAlgorithm]hashFunc{
+ hashAlgoSHA1: func(data []byte) Hash {
+ sum := sha1.Sum(data)
+ var h Hash
+ copy(h.data[:], sum[:])
+ h.algo = hashAlgoSHA1
+ return h
+ },
+ hashAlgoSHA256: func(data []byte) Hash {
+ sum := sha256.Sum256(data)
+ var h Hash
+ copy(h.data[:], sum[:])
+ h.algo = hashAlgoSHA256
+ return h
+ },
+}
+
const (
hashAlgoUnknown hashAlgorithm = iota
hashAlgoSHA1
@@ -50,24 +70,6 @@ type Hash struct {
// hashFunc is a function that computes a hash from input data.
type hashFunc func([]byte) Hash
-// hashFuncs maps hash algorithm to hash function.
-var hashFuncs = map[hashAlgorithm]hashFunc{
- hashAlgoSHA1: func(data []byte) Hash {
- sum := sha1.Sum(data)
- var h Hash
- copy(h.data[:], sum[:])
- h.algo = hashAlgoSHA1
- return h
- },
- hashAlgoSHA256: func(data []byte) Hash {
- sum := sha256.Sum256(data)
- var h Hash
- copy(h.data[:], sum[:])
- h.algo = hashAlgoSHA256
- return h
- },
-}
-
// String returns a hexadecimal string representation of the hash.
func (hash Hash) String() string {
size := hash.algo.size()