aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus@mullvad.net>2022-04-13 15:33:26 +0200
committerRasmus Dahlberg <rasmus@mullvad.net>2022-04-13 15:33:26 +0200
commit6d9aea74c19c350802cb52787f1450bd751557f5 (patch)
treedec77a7bd08a389669bac0980cb10d42b2f22425 /internal
parent7d0cbd048ce258893b2420e7f3568f4d0cf6aaf8 (diff)
start on internal helpers
Diffstat (limited to 'internal')
-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
+}