-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa-private.go
93 lines (79 loc) · 2.2 KB
/
rsa-private.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
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parlca
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"github.com/haraldrudell/parl"
"github.com/haraldrudell/parl/perrors"
)
const (
rsaDefaultBits = 2048
)
type RsaPrivateKey struct {
// Decrypt(rand io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error)
// Equal(x crypto.PrivateKey) bool
// Precompute()
// Public() crypto.PublicKey
// Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error)
// Size() int
// Validate() error
rsa.PrivateKey
}
func NewRsa() (privateKey parl.PrivateKey, err error) {
return NewRsaBits(rsaDefaultBits)
}
func NewRsaBits(bits int) (privateKey parl.PrivateKey, err error) {
var rsaPrivateKey *rsa.PrivateKey
if rsaPrivateKey, err = rsa.GenerateKey(rand.Reader, bits); perrors.IsPF(&err, "rsa.GenerateKey: %w", err) {
return
}
privateKey = &RsaPrivateKey{PrivateKey: *rsaPrivateKey}
return
}
func (key *RsaPrivateKey) Algo() (algo x509.PublicKeyAlgorithm) {
return x509.RSA
}
func (key *RsaPrivateKey) DER() (bytes parl.PrivateKeyDer, err error) {
if bytes, err = x509.MarshalPKCS8PrivateKey(&key.PrivateKey); err != nil {
err = perrors.Errorf("x509.MarshalPKCS8PrivateKey: '%w'", err)
}
return
}
func (key *RsaPrivateKey) DERe() (privateKeyDer parl.PrivateKeyDer) {
var err error
if privateKeyDer, err = key.DER(); err != nil {
panic(err)
}
return
}
func (key *RsaPrivateKey) PEM() (pemBytes parl.PemBytes, err error) {
block := pem.Block{
Type: pemPrivateKeyType,
}
if block.Bytes, err = key.DER(); err != nil {
return
}
pemBytes = append([]byte(PemText(block.Bytes)), pem.EncodeToMemory(&block)...)
return
}
func (key *RsaPrivateKey) PEMe() (pemBytes parl.PemBytes) {
var err error
if pemBytes, err = key.PEM(); err != nil {
panic(err)
}
return
}
func (key *RsaPrivateKey) PublicKey() (publicKey parl.PublicKey) {
return &RsaPublicKey{PublicKey: key.PrivateKey.PublicKey}
}
func (key *RsaPrivateKey) Validate() (err error) {
if key.PrivateKey.D == nil {
return perrors.New("rsa priovate key uninitialized")
}
return key.PrivateKey.Validate()
}