diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2025-11-24 08:04:19 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2025-11-24 08:55:49 +0000 |
| commit | 080caeb9e30c884ab78354eae80c53cc47d8d49b (patch) | |
| tree | f6cc8b3627b869d96e3c0d772d88299ce95dce77 /pkg | |
| parent | 4fb8ef376b21a14e0ed92c40c92913ac567bd2a3 (diff) | |
pkg/hash: support hashing strings
Currently passing wrong types (e.g. a string) to hash.Hash/String leads
to silent misbehavior: the part is ignored in the hash.
This is a very bad failure mode. Panic loudly.
And support hashing strings.
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/hash/hash.go | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/pkg/hash/hash.go b/pkg/hash/hash.go index 350d3a3e8..6b3005fc3 100644 --- a/pkg/hash/hash.go +++ b/pkg/hash/hash.go @@ -16,7 +16,12 @@ type Sig [sha1.Size]byte func Hash(pieces ...any) Sig { h := sha1.New() for _, data := range pieces { - binary.Write(h, binary.LittleEndian, data) + if str, ok := data.(string); ok { + data = []byte(str) + } + if err := binary.Write(h, binary.LittleEndian, data); err != nil { + panic(err) + } } var sig Sig copy(sig[:], h.Sum(nil)) |
