From 3db06e4b2030812a6c1dc8fa2b68c5836ddd00d2 Mon Sep 17 00:00:00 2001 From: Adam Schmideg Date: Wed, 4 Mar 2020 14:21:15 +0100 Subject: [PATCH] More idiomatic way to check for trailing whitespace --- crypto/crypto.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crypto/crypto.go b/crypto/crypto.go index ac8affbde2..4b1aa3176b 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -17,6 +17,7 @@ package crypto import ( + "bytes" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" @@ -178,15 +179,12 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) { if err != nil { return nil, err } - // Check line ending - maybeLineEnding := buf[64:] - for _, ch := range maybeLineEnding { - if ch != '\n' && ch != '\r' { - return nil, fmt.Errorf("expected 64 bytes, got %v", size) - } + buf = bytes.TrimSpace(buf) + if len(buf) != 64 { + return nil, fmt.Errorf("expected 64 bytes, got %v", len(buf)) } - key, err := hex.DecodeString(string(buf[:64])) + key, err := hex.DecodeString(string(buf)) if err != nil { return nil, err }