diff options
| author | 2026-01-19 01:03:23 +0800 | |
|---|---|---|
| committer | 2026-01-19 01:03:23 +0800 | |
| commit | 32f48e18896061529f038eaf527f2eec0490f5df (patch) | |
| tree | b6f09abd46b297b2e929e29d98abefb02b43b6ff | |
| parent | go.mod: Use the codeberg import path (diff) | |
| signature | No signature | |
hash: Document maxHashSize properly
| -rw-r--r-- | hash.go | 40 |
1 files changed, 21 insertions, 19 deletions
@@ -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() |
