From 6d9aea74c19c350802cb52787f1450bd751557f5 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Wed, 13 Apr 2022 15:33:26 +0200 Subject: start on internal helpers --- internal/util/crypto.go | 20 ++++++++++++++++++++ internal/util/io.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 internal/util/crypto.go create mode 100644 internal/util/io.go (limited to 'internal/util') diff --git a/internal/util/crypto.go b/internal/util/crypto.go new file mode 100644 index 0000000..2a29628 --- /dev/null +++ b/internal/util/crypto.go @@ -0,0 +1,20 @@ +package util + +import ( + "crypto" + "crypto/ed25519" + "fmt" + + "git.sigsum.org/sigsum-go/pkg/hex" +) + +func SignerFromHex(s string) (crypto.Signer, error) { + b, err := hex.Deserialize(s) + if err != nil { + return nil, fmt.Errorf("util: parse key: %v", err) + } + if len(b) != ed25519.PrivateKeySize { + return nil, fmt.Errorf("util: invalid private key size %d", len(b)) + } + return ed25519.PrivateKey(b), nil +} diff --git a/internal/util/io.go b/internal/util/io.go new file mode 100644 index 0000000..143a520 --- /dev/null +++ b/internal/util/io.go @@ -0,0 +1,30 @@ +package util + +import ( + "bytes" + "fmt" + "io/ioutil" + "os" + + "git.sigsum.org/sigsum-go/pkg/hex" +) + +func BytesFromStdin() ([]byte, error) { + b, err := ioutil.ReadAll(os.Stdin) + if err != nil { + return nil, fmt.Errorf("util: read stdin: %v", err) + } + return b, nil +} + +func HexFromStdin() ([]byte, error) { + b, err := BytesFromStdin() + if err != nil { + return nil, err + } + b, err = hex.Deserialize(string(bytes.TrimSpace(b))) + if err != nil { + return nil, fmt.Errorf("util: parse stdin: %v", err) + } + return b, nil +} -- cgit v1.2.3