aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/hash.go
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2023-03-07 23:31:29 +0000
committerPaulo Gomes <pjbgf@linux.com>2023-03-08 00:14:59 +0000
commit9822ad8573e374421a79c096d8f1dfa91366fb02 (patch)
treee3adb42646511378112cb05461bb8154f010ddee /plumbing/hash.go
parent99e2f85843878671b028d4d01bd4668676226dd1 (diff)
downloadgo-git-9822ad8573e374421a79c096d8f1dfa91366fb02.tar.gz
*: Support variable length plumbing.Hash
The variable length for plumbing.Hash is defined at build time, blocked by tag sha256. This approach was a trade-off between keeping backwards compatibility while making progress towards supporting SHA256 with a small amount of changes. Relates to the SHA256 implementation, defined in #706. Signed-off-by: Paulo Gomes <pjbgf@linux.com>
Diffstat (limited to 'plumbing/hash.go')
-rw-r--r--plumbing/hash.go14
1 files changed, 7 insertions, 7 deletions
diff --git a/plumbing/hash.go b/plumbing/hash.go
index 2fab759..39bb73f 100644
--- a/plumbing/hash.go
+++ b/plumbing/hash.go
@@ -2,7 +2,6 @@ package plumbing
import (
"bytes"
- "crypto"
"encoding/hex"
"sort"
"strconv"
@@ -11,7 +10,7 @@ import (
)
// Hash SHA1 hashed content
-type Hash [20]byte
+type Hash [hash.Size]byte
// ZeroHash is Hash with value zero
var ZeroHash Hash
@@ -47,7 +46,7 @@ type Hasher struct {
}
func NewHasher(t ObjectType, size int64) Hasher {
- h := Hasher{hash.New(crypto.SHA1)}
+ h := Hasher{hash.New(hash.CryptoType)}
h.Write(t.Bytes())
h.Write([]byte(" "))
h.Write([]byte(strconv.FormatInt(size, 10)))
@@ -75,10 +74,11 @@ func (p HashSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// IsHash returns true if the given string is a valid hash.
func IsHash(s string) bool {
- if len(s) != 40 {
+ switch len(s) {
+ case hash.HexSize:
+ _, err := hex.DecodeString(s)
+ return err == nil
+ default:
return false
}
-
- _, err := hex.DecodeString(s)
- return err == nil
}