-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
72 lines (56 loc) · 1.53 KB
/
rsa.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
package cipher
import (
"crypto/rsa"
"crypto/x509"
"io"
"github.com/flily/go-ssl/common/encoder"
)
type RSAPrivateKey struct {
rsa.PrivateKey
}
func GenerateRSAKey(random io.Reader, bits int) (*RSAPrivateKey, error) {
privateKey, err := rsa.GenerateKey(random, bits)
if err != nil {
return nil, err
}
k := &RSAPrivateKey{
PrivateKey: *privateKey,
}
return k, nil
}
func (k *RSAPrivateKey) DER() []byte {
return k.PKCS8PrivateKey()
}
func (k *RSAPrivateKey) PEM() []byte {
return k.PKCS8PrivateKeyPEM()
}
func (k *RSAPrivateKey) PKCS1PrivateKey() []byte {
return x509.MarshalPKCS1PrivateKey(&k.PrivateKey)
}
func (k *RSAPrivateKey) PKCS1PrivateKeyPEM() []byte {
content := k.PKCS1PrivateKey()
return encoder.PEMEncode("RSA PRIVATE KEY", content)
}
func (k *RSAPrivateKey) PKCS1PublicKey() []byte {
return x509.MarshalPKCS1PublicKey(&k.PrivateKey.PublicKey)
}
func (k *RSAPrivateKey) PKCS1PublicKeyPEM() []byte {
content := k.PKCS1PublicKey()
return encoder.PEMEncode("RSA PUBLIC KEY", content)
}
func (k *RSAPrivateKey) PKCS8PrivateKey() []byte {
content, _ := x509.MarshalPKCS8PrivateKey(&k.PrivateKey)
return content
}
func (k *RSAPrivateKey) PKCS8PrivateKeyPEM() []byte {
content := k.PKCS8PrivateKey()
return encoder.PEMEncode("PRIVATE KEY", content)
}
func (k *RSAPrivateKey) PKIXPrivateKey() []byte {
content, _ := x509.MarshalPKIXPublicKey(&k.PrivateKey.PublicKey)
return content
}
func (k *RSAPrivateKey) PKIXPrivateKeyPEM() []byte {
content := k.PKIXPrivateKey()
return encoder.PEMEncode("PUBLIC KEY", content)
}