-
Notifications
You must be signed in to change notification settings - Fork 2
/
rsa.go
126 lines (115 loc) · 2.75 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
package rsa
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"crypto/x509"
"encoding/pem"
"fmt"
)
// Encrypt ...
func Encrypt(msg, publicKeyBytes []byte) ([]byte, error) {
pk, err := BytesToPublicKey(publicKeyBytes)
if err != nil {
return nil, err
}
return EncryptWithPublicKey(msg, pk)
}
// Decrypt ...
func Decrypt(ciphered, privateKeyBytes []byte) ([]byte, error) {
sk, err := BytesToPrivateKey(privateKeyBytes)
if err != nil {
return nil, err
}
return DecryptWithPrivateKey(ciphered, sk)
}
//--- utilities
// GenerateKeyPair generates a new key pair
func GenerateKeyPair(bits int) (sk *rsa.PrivateKey, pk *rsa.PublicKey, err error) {
privkey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return
}
sk = privkey
pk = &privkey.PublicKey
return
}
// PrivateKeyToBytes transforms private key to bytes
func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte {
privBytes := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
},
)
return privBytes
}
// PublicKeyToBytes transforms public key to bytes
func PublicKeyToBytes(pub *rsa.PublicKey) ([]byte, error) {
pubASN1, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return nil, err
}
pubBytes := pem.EncodeToMemory(&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pubASN1,
})
return pubBytes, nil
}
// BytesToPrivateKey converts bytes to private key
func BytesToPrivateKey(priv []byte) (sk *rsa.PrivateKey, err error) {
block, _ := pem.Decode(priv)
enc := x509.IsEncryptedPEMBlock(block)
b := block.Bytes
if enc {
b, err = x509.DecryptPEMBlock(block, nil)
if err != nil {
return
}
}
key, err := x509.ParsePKCS1PrivateKey(b)
if err != nil {
return
}
return key, nil
}
// BytesToPublicKey converts bytes to public key
func BytesToPublicKey(pub []byte) (pk *rsa.PublicKey, err error) {
block, _ := pem.Decode(pub)
enc := x509.IsEncryptedPEMBlock(block)
b := block.Bytes
if enc {
b, err = x509.DecryptPEMBlock(block, nil)
if err != nil {
return
}
}
ifc, err := x509.ParsePKIXPublicKey(b)
if err != nil {
return
}
key, ok := ifc.(*rsa.PublicKey)
if !ok {
err = fmt.Errorf("not ok")
return
}
return key, nil
}
// EncryptWithPublicKey encrypts data with public key
func EncryptWithPublicKey(msg []byte, pub *rsa.PublicKey) ([]byte, error) {
hash := sha512.New()
ciphertext, err := rsa.EncryptOAEP(hash, rand.Reader, pub, msg, nil)
if err != nil {
return nil, err
}
return ciphertext, nil
}
// DecryptWithPrivateKey decrypts data with private key
func DecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey) ([]byte, error) {
hash := sha512.New()
plaintext, err := rsa.DecryptOAEP(hash, rand.Reader, priv, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}