forked from forgoer/openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
152 lines (124 loc) · 3.09 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package openssl
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"io"
)
// RSAGenerateKey generate RSA private key
func RSAGenerateKey(bits int, out io.Writer) error {
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
privateBlock := pem.Block{Type: "RSA PRIVATE KEY", Bytes: X509PrivateKey}
return pem.Encode(out, &privateBlock)
}
// RSAGeneratePublicKey generate RSA public key
func RSAGeneratePublicKey(priKey []byte, out io.Writer) error {
block, _ := pem.Decode(priKey)
if block == nil {
return errors.New("key is invalid format")
}
// x509 parse
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return err
}
publicKey := privateKey.PublicKey
X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
if err != nil {
return err
}
publicBlock := pem.Block{Type: "RSA PUBLIC KEY", Bytes: X509PublicKey}
return pem.Encode(out, &publicBlock)
}
// RSAEncrypt RSA encrypt
func RSAEncrypt(src, pubKey []byte) ([]byte, error) {
block, _ := pem.Decode(pubKey)
if block == nil {
return nil, errors.New("key is invalid format")
}
// x509 parse
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
publicKey, ok := publicKeyInterface.(*rsa.PublicKey)
if !ok {
return nil, errors.New("the kind of key is not a rsa.PublicKey")
}
// encrypt
dst, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, src)
if err != nil {
return nil, err
}
return dst, nil
}
// RSADecrypt RSA decrypt
func RSADecrypt(src, priKey []byte) ([]byte, error) {
block, _ := pem.Decode(priKey)
if block == nil {
return nil, errors.New("key is invalid format")
}
// x509 parse
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
dst, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, src)
if err != nil {
return nil, err
}
return dst, nil
}
// RSASign RSA sign
func RSASign(src []byte, priKey []byte, hash crypto.Hash) ([]byte, error) {
block, _ := pem.Decode(priKey)
if block == nil {
return nil, errors.New("key is invalid format")
}
// x509 parse
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
h := hash.New()
_, err = h.Write(src)
if err != nil {
return nil, err
}
bytes := h.Sum(nil)
sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey, hash, bytes)
if err != nil {
return nil, err
}
return sign, nil
}
// RSAVerify RSA verify
func RSAVerify(src, sign, pubKey []byte, hash crypto.Hash) error {
block, _ := pem.Decode(pubKey)
if block == nil {
return errors.New("key is invalid format")
}
// x509 parse
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return err
}
publicKey, ok := publicKeyInterface.(*rsa.PublicKey)
if !ok {
return errors.New("the kind of key is not a rsa.PublicKey")
}
h := hash.New()
_, err = h.Write(src)
if err != nil {
return err
}
bytes := h.Sum(nil)
return rsa.VerifyPKCS1v15(publicKey, hash, bytes, sign)
}