/
cryptosigner.go
53 lines (42 loc) · 949 Bytes
/
cryptosigner.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package bccsp
import (
"crypto"
"crypto/x509"
"errors"
"io"
)
type cryptoSigner struct {
csp BCCSP
sk Key
pk interface{}
}
func NewCryptoSigner(csp BCCSP, key Key) (crypto.Signer, error) {
if csp == nil {
return nil, errors.New("bccsp instance must be different from nil")
}
if key == nil {
return nil, errors.New("must provide secret key")
}
if key.Symmetric() {
return nil, errors.New("secret key must be asymmetric")
}
pub, err := key.PublicKey()
if err != nil {
return nil, err
}
pkBytes, err := pub.Bytes()
if err != nil {
return nil, err
}
pk, err := x509.ParsePKIXPublicKey(pkBytes)
if err != nil {
return nil, err
}
return &cryptoSigner{csp: csp, sk: key, pk: pk}, nil
}
func (c *cryptoSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
return c.csp.Sign(c.sk, digest, opts)
}
func (c *cryptoSigner) Public() crypto.PublicKey {
return c.pk
}