-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
115 lines (95 loc) · 2.61 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
package rsa
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"errors"
)
type RSA struct {
privateKey *rsa.PrivateKey
publicKey *rsa.PublicKey
//
privateKeyString string
}
func New(privateKey string) (*RSA, error) {
privateKeyDecoded, err := base64.StdEncoding.DecodeString(privateKey)
if err != nil {
return nil, errors.New("invalid private key(1): " + err.Error())
}
_privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyDecoded)
if err != nil {
return nil, errors.New("invalid private key(2): " + err.Error())
}
publicKeyDecoded, err := GeneratePublicKey(privateKeyDecoded)
if err != nil {
return nil, errors.New("invalid private key(3): " + err.Error())
}
_publicKey, err := x509.ParsePKCS1PublicKey(publicKeyDecoded)
if err != nil {
return nil, errors.New("invalid public key(2): " + err.Error())
}
return &RSA{
privateKeyString: privateKey,
privateKey: _privateKey,
publicKey: _publicKey,
}, nil
}
func (r *RSA) Encrypt(plainbytes []byte) (cipherbytes []byte, err error) {
cipherbytes, err = rsa.EncryptPKCS1v15(rand.Reader, r.publicKey, plainbytes)
return
}
func (r *RSA) Decrypt(cipherbytes []byte) (plainbytes []byte, err error) {
plainbytes, err = rsa.DecryptPKCS1v15(rand.Reader, r.privateKey, cipherbytes)
return
}
func (r *RSA) Sign(message []byte) (signature []byte, err error) {
hash := sha256.New()
_, err = hash.Write(message)
if err != nil {
panic(err)
}
sum := hash.Sum(nil)
signature, err = rsa.SignPKCS1v15(rand.Reader, r.privateKey, crypto.SHA256, sum)
return
}
func (r *RSA) Verify(message, signature []byte) (ok bool, err error) {
hash := sha256.New()
_, err = hash.Write(message)
if err != nil {
panic(err)
}
sum := hash.Sum(nil)
err = rsa.VerifyPKCS1v15(r.publicKey, crypto.SHA256, sum, signature)
if err != nil {
return
}
return true, nil
}
func (r *RSA) GetPublickKey() string {
s, _ := GeneratePublicKeyString(r.privateKeyString)
return s
}
//
type RSAEncryptor struct {
publicKey *rsa.PublicKey
}
func NewEncryptor(publicKey string) (*RSAEncryptor, error) {
publicKeyDecoded, err := base64.StdEncoding.DecodeString(publicKey)
if err != nil {
return nil, errors.New("invalid public key(1): " + err.Error())
}
_publicKey, err := x509.ParsePKCS1PublicKey(publicKeyDecoded)
if err != nil {
return nil, errors.New("invalid public key(2): " + err.Error())
}
return &RSAEncryptor{
publicKey: _publicKey,
}, nil
}
func (r *RSAEncryptor) Encrypt(plainbytes []byte) (cipherbytes []byte, err error) {
cipherbytes, err = rsa.EncryptPKCS1v15(rand.Reader, r.publicKey, plainbytes)
return
}