More idiomatic way to check for trailing whitespace

This commit is contained in:
Adam Schmideg 2020-03-04 14:21:15 +01:00
parent fe0bc499bb
commit 3db06e4b20

View file

@ -17,6 +17,7 @@
package crypto package crypto
import ( import (
"bytes"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
@ -178,15 +179,12 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Check line ending buf = bytes.TrimSpace(buf)
maybeLineEnding := buf[64:] if len(buf) != 64 {
for _, ch := range maybeLineEnding { return nil, fmt.Errorf("expected 64 bytes, got %v", len(buf))
if ch != '\n' && ch != '\r' {
return nil, fmt.Errorf("expected 64 bytes, got %v", size)
}
} }
key, err := hex.DecodeString(string(buf[:64])) key, err := hex.DecodeString(string(buf))
if err != nil { if err != nil {
return nil, err return nil, err
} }