-
Notifications
You must be signed in to change notification settings - Fork 22
/
ecdh_from.go
68 lines (53 loc) · 1.35 KB
/
ecdh_from.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
package ecdh
import (
"crypto/rand"
"github.com/deatil/go-cryptobin/dhd/ecdh"
)
// 私钥
func (this Ecdh) FromPrivateKey(key []byte) Ecdh {
parsedKey, err := this.ParsePrivateKeyFromPEM(key)
if err != nil {
this.Error = err
return this
}
this.privateKey = parsedKey.(*ecdh.PrivateKey)
return this
}
// 私钥带密码
func (this Ecdh) FromPrivateKeyWithPassword(key []byte, password string) Ecdh {
parsedKey, err := this.ParsePrivateKeyFromPEMWithPassword(key, password)
if err != nil {
this.Error = err
return this
}
this.privateKey = parsedKey.(*ecdh.PrivateKey)
return this
}
// 公钥
func (this Ecdh) FromPublicKey(key []byte) Ecdh {
parsedKey, err := this.ParsePublicKeyFromPEM(key)
if err != nil {
this.Error = err
return this
}
this.publicKey = parsedKey.(*ecdh.PublicKey)
return this
}
// 生成密钥
func (this Ecdh) GenerateKey(curve string) Ecdh {
var c ecdh.Curve
switch curve {
case "P521":
c = ecdh.P521()
case "P384":
c = ecdh.P384()
case "P256":
c = ecdh.P256()
case "P224":
c = ecdh.P224()
default:
c = ecdh.P224()
}
this.privateKey, this.publicKey, this.Error = ecdh.GenerateKey(c, rand.Reader)
return this
}