This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa_encryptor.go
140 lines (111 loc) · 3.1 KB
/
rsa_encryptor.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
/*
* This file is part of the dupman/encryptor project.
*
* (c) 2022. dupman <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Written by Temuri Takalandze <[email protected]>
*/
package encryptor
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
)
type RSAEncryptor struct {
privateKey *rsa.PrivateKey
publicKey *rsa.PublicKey
}
const keySize = 2048
var errUnableToDecodeKey = errors.New("unable to decode key")
func NewRSAEncryptor() *RSAEncryptor {
return &RSAEncryptor{}
}
// SetPrivateKey sets the Private Key value.
func (e *RSAEncryptor) SetPrivateKey(privateKey string) (err error) {
privateKeyDecoded, err := base64.StdEncoding.DecodeString(privateKey)
if err != nil {
return err
}
block, _ := pem.Decode(privateKeyDecoded)
e.privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return err
}
return nil
}
// PrivateKey gets the Private Key value.
func (e *RSAEncryptor) PrivateKey() (privateKey string) {
privateKeyBytes := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(e.privateKey),
},
)
return base64.StdEncoding.EncodeToString(privateKeyBytes)
}
// SetPublicKey sets the Public Key value.
func (e *RSAEncryptor) SetPublicKey(publicKey string) (err error) {
publicKeyDecoded, err := base64.StdEncoding.DecodeString(publicKey)
if err != nil {
return err
}
block, _ := pem.Decode(publicKeyDecoded)
ifc, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return err
}
if key, ok := ifc.(*rsa.PublicKey); ok {
e.publicKey = key
return nil
}
return errUnableToDecodeKey
}
// PublicKey gets the Public Key value.
func (e *RSAEncryptor) PublicKey() (publicKey string, err error) {
ASN1, err := x509.MarshalPKIXPublicKey(e.publicKey)
if err != nil {
return publicKey, err
}
publicKeyBytes := pem.EncodeToMemory(&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: ASN1,
})
return base64.StdEncoding.EncodeToString(publicKeyBytes), err
}
// GenerateKeyPair generates a new key pair.
func (e *RSAEncryptor) GenerateKeyPair() (err error) {
keyPair, err := rsa.GenerateKey(rand.Reader, keySize)
if err != nil {
return err
}
e.privateKey = keyPair
e.publicKey = &keyPair.PublicKey
return nil
}
// Encrypt encrypts data with given public key.
func (e *RSAEncryptor) Encrypt(text string) (encrypted string, err error) {
encryptedBytes, err := rsa.EncryptOAEP(sha512.New(), rand.Reader, e.publicKey, []byte(text), nil)
if err != nil {
return encrypted, err
}
return base64.StdEncoding.EncodeToString(encryptedBytes), err
}
// Decrypt decrypts data with given private key.
func (e *RSAEncryptor) Decrypt(encrypted string) (decrypted string, err error) {
encryptedBytes, err := base64.StdEncoding.DecodeString(encrypted)
if err != nil {
return "", err
}
decryptedBytes, err := rsa.DecryptOAEP(sha512.New(), rand.Reader, e.privateKey, encryptedBytes, nil)
if err != nil {
return decrypted, err
}
return string(decryptedBytes), err
}