aboutsummaryrefslogtreecommitdiff
path: root/internal/util/crypto.go
blob: a8c8face184d82104fc2154c7ee3079c74456a21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package util

import (
	"crypto"
	"crypto/ed25519"
	"fmt"
	"io/ioutil"

	"git.sigsum.org/sigsum-go/pkg/hex"
	"git.sigsum.org/sigsum-go/pkg/types"
)

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
}

func FileHash(path string) (*types.Hash, error) {
	b, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("util: read file %s: %v", path, err)
	}
	return types.HashFn(b), nil
}