-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrsa.go
55 lines (40 loc) · 829 Bytes
/
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
package p2p
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
)
type RSA struct {
key *rsa.PrivateKey
}
func NewRSA() (r *RSA, err error) {
r = &RSA{}
r.key, err = rsa.GenerateKey(rand.Reader, 2048)
return
}
func (r *RSA) PublicKey() (pk PublicKey) {
pk = PublicKey{
Key: r.key.PublicKey,
}
return
}
func (r *RSA) PrivateKey() (pk PrivateKey) {
pk = PrivateKey{
key: *r.key,
}
return
}
type PublicKey struct {
Key rsa.PublicKey
}
func (pk PublicKey) Encode(ck CipherKey) (cck CryptCipherKey, err error) {
cck, err = rsa.EncryptOAEP(sha512.New(), rand.Reader, &pk.Key, ck, nil)
return
}
type PrivateKey struct {
key rsa.PrivateKey
}
func (pk PrivateKey) Decode(cck CryptCipherKey) (ck CipherKey, err error) {
ck, err = rsa.DecryptOAEP(sha512.New(), rand.Reader, &pk.key, cck, nil)
return
}