Skip to content

Commit f40c61d

Browse files
committedJan 4, 2023
Update hash name check to be case insensitive
Some SDPs emit uppercase hash names. This change makes the comparison case insensitive.
1 parent 3026357 commit f40c61d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed
 

‎pkg/crypto/fingerprint/hash.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fingerprint
33
import (
44
"crypto"
55
"errors"
6+
"strings"
67
)
78

89
var errInvalidHashAlgorithm = errors.New("fingerprint: invalid hash algorithm")
@@ -20,7 +21,7 @@ func nameToHash() map[string]crypto.Hash {
2021

2122
// HashFromString allows looking up a hash algorithm by it's string representation
2223
func HashFromString(s string) (crypto.Hash, error) {
23-
if h, ok := nameToHash()[s]; ok {
24+
if h, ok := nameToHash()[strings.ToLower(s)]; ok {
2425
return h, nil
2526
}
2627
return 0, errInvalidHashAlgorithm

‎pkg/crypto/fingerprint/hash_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ func TestHashFromString(t *testing.T) {
2222
t.Errorf("Expected hash ID of %d, got %d", int(crypto.SHA512), int(h))
2323
}
2424
})
25+
t.Run("ValidCaseInsensitiveHashAlgorithm", func(t *testing.T) {
26+
h, err := HashFromString("SHA-512")
27+
if err != nil {
28+
t.Fatalf("Unexpected error for valid hash name, got '%v'", err)
29+
}
30+
if h != crypto.SHA512 {
31+
t.Errorf("Expected hash ID of %d, got %d", int(crypto.SHA512), int(h))
32+
}
33+
})
2534
}
2635

2736
func TestStringFromHash_Roundtrip(t *testing.T) {

0 commit comments

Comments
 (0)
Please sign in to comment.