aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdnan Maolood <[email protected]>2021-03-06 15:48:51 -0500
committerAdnan Maolood <[email protected]>2021-03-06 15:49:11 -0500
commita5493b708aea740c81e31f5db1e82faaa43d147c (patch)
tree841d3a63afa1fff8fb45f0721f76bc27ff7b2840
parenttofu: Use base64-encoded sha256 fingerprints (diff)
downloadgo-gemini-a5493b708aea740c81e31f5db1e82faaa43d147c.tar.xz
go-gemini-a5493b708aea740c81e31f5db1e82faaa43d147c.zip
tofu: Fix known host unmarshaling
-rw-r--r--tofu/tofu.go29
1 files changed, 6 insertions, 23 deletions
diff --git a/tofu/tofu.go b/tofu/tofu.go
index 554b260..6a7a15e 100644
--- a/tofu/tofu.go
+++ b/tofu/tofu.go
@@ -7,7 +7,6 @@ import (
"crypto/sha256"
"crypto/x509"
"encoding/base64"
- "errors"
"fmt"
"io"
"os"
@@ -132,6 +131,9 @@ func (k *KnownHosts) Parse(r io.Reader) error {
if err != nil {
continue
}
+ if h.Algorithm != "sha256" {
+ continue
+ }
k.hosts[h.Hostname] = h
}
@@ -317,32 +319,13 @@ func (h Host) String() string {
// UnmarshalText unmarshals the host from the provided text.
func (h *Host) UnmarshalText(text []byte) error {
- const format = "hostname algorithm fingerprint"
-
parts := bytes.Split(text, []byte(" "))
if len(parts) != 3 {
- return fmt.Errorf("expected the format %q", format)
- }
-
- if len(parts[0]) == 0 {
- return errors.New("empty hostname")
+ return fmt.Errorf("expected the format 'hostname algorithm fingerprint'")
}
h.Hostname = string(parts[0])
-
- algorithm := string(parts[1])
- if algorithm != "sha256" {
- return fmt.Errorf("unsupported algorithm %q", algorithm)
- }
-
- h.Algorithm = algorithm
-
- fingerprint, err := base64.StdEncoding.DecodeString(string(parts[2]))
- if err != nil {
- return err
- }
-
- h.Fingerprint = string(fingerprint)
-
+ h.Algorithm = string(parts[1])
+ h.Fingerprint = string(parts[2])
return nil
}