-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
102 lines (88 loc) · 2.29 KB
/
crypto.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package solidproxy
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
)
// Signer creates signatures that verify against a public key.
type Signer interface {
Sign(data []byte) ([]byte, error)
}
// Verifier verifies signatures against a public key.
type Verifier interface {
Verify(data []byte, sig []byte) error
}
type rsaPubKey struct {
*rsa.PublicKey
}
type rsaPrivKey struct {
*rsa.PrivateKey
}
// ParseRSAPublicPEMKey parses a PEM encoded private key and returns a new verifier object
func ParseRSAPublicPEMKey(pemBytes []byte) (Verifier, error) {
block, _ := pem.Decode(pemBytes)
if block == nil {
return nil, errors.New("No key found")
}
var rawkey interface{}
switch block.Type {
case "RSA PUBLIC KEY", "PUBLIC KEY":
rsa, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
rawkey = rsa
default:
return nil, fmt.Errorf("Unsupported key type %q", block.Type)
}
return newVerifierFromKey(rawkey)
}
// ParseRSAPrivatePEMKey parses a PEM encoded private key and returns a Signer.
func ParseRSAPrivatePEMKey(pemBytes []byte) (Signer, error) {
block, _ := pem.Decode(pemBytes)
if block == nil {
return nil, errors.New("No key found or could not decode PEM key")
}
var rawkey interface{}
switch block.Type {
case "RSA PRIVATE KEY", "PRIVATE KEY":
rsa, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
rawkey = rsa
default:
return nil, fmt.Errorf("Unsupported key type %q", block.Type)
}
return newSignerFromKey(rawkey)
}
func newSignerFromKey(k interface{}) (Signer, error) {
var sKey Signer
switch t := k.(type) {
case *rsa.PrivateKey:
// we only support one type
sKey = &rsaPrivKey{t}
}
return sKey, nil
}
func newVerifierFromKey(k interface{}) (Verifier, error) {
var vKey Verifier
switch t := k.(type) {
case *rsa.PublicKey:
// we only support one type
vKey = &rsaPubKey{t}
}
return vKey, nil
}
// Sign signs data with rsa-sha256
func (r *rsaPrivKey) Sign(data []byte) ([]byte, error) {
return rsa.SignPKCS1v15(rand.Reader, r.PrivateKey, crypto.SHA1, data)
}
// Verify verifies the message using a rsa-sha256 signature
func (r *rsaPubKey) Verify(message []byte, sig []byte) error {
return rsa.VerifyPKCS1v15(r.PublicKey, crypto.SHA1, message, sig)
}