-
Notifications
You must be signed in to change notification settings - Fork 25
/
encryption.go
48 lines (36 loc) · 1 KB
/
encryption.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
package ecdsa
import (
"errors"
"crypto/rand"
"github.com/deatil/go-cryptobin/ecc"
)
// 公钥加密
// Ecdsa 核心为对称加密
func (this Ecdsa) Encrypt() Ecdsa {
if this.publicKey == nil {
err := errors.New("Ecdsa: publicKey error.")
return this.AppendError(err)
}
publicKey := ecc.ImportECDSAPublicKey(this.publicKey)
parsedData, err := ecc.Encrypt(rand.Reader, publicKey, this.data, nil, nil)
if err != nil {
return this.AppendError(err)
}
this.parsedData = parsedData
return this
}
// 私钥解密
// Ecdsa 核心为对称加密
func (this Ecdsa) Decrypt() Ecdsa {
if this.privateKey == nil {
err := errors.New("Ecdsa: privateKey error.")
return this.AppendError(err)
}
privateKey := ecc.ImportECDSAPrivateKey(this.privateKey)
parsedData, err := ecc.Decrypt(privateKey, this.data, nil, nil)
if err != nil {
return this.AppendError(err)
}
this.parsedData = parsedData
return this
}