aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/util/crypto.go20
-rw-r--r--internal/util/io.go30
2 files changed, 50 insertions, 0 deletions
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
+}