Skip to content

Commit 59f185c

Browse files
committed
knownhosts: fix hashed hostname component count in error message
Correct the component splitting in the nextWord function to omit the initial empty element when decoding the pipe-separated hostname hash. Previously, the error message incorrectly counted this empty element, leading to misleading errors like: knownhosts: got 3 components, want 3 This change makes the component split start from index 1. The existing tests cover the changed code. Signed-off-by: Kimmo Lehto <[email protected]>
1 parent d042a39 commit 59f185c

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

ssh/knownhosts/knownhosts.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,17 @@ func decodeHash(encoded string) (hashType string, salt, hash []byte, err error)
481481
err = errors.New("knownhosts: hashed host must start with '|'")
482482
return
483483
}
484-
components := strings.Split(encoded, "|")
485-
if len(components) != 4 {
484+
components := strings.Split(encoded[1:], "|")
485+
if len(components) != 3 {
486486
err = fmt.Errorf("knownhosts: got %d components, want 3", len(components))
487487
return
488488
}
489489

490-
hashType = components[1]
491-
if salt, err = base64.StdEncoding.DecodeString(components[2]); err != nil {
490+
hashType = components[0]
491+
if salt, err = base64.StdEncoding.DecodeString(components[1]); err != nil {
492492
return
493493
}
494-
if hash, err = base64.StdEncoding.DecodeString(components[3]); err != nil {
494+
if hash, err = base64.StdEncoding.DecodeString(components[2]); err != nil {
495495
return
496496
}
497497
return

0 commit comments

Comments
 (0)