-
Notifications
You must be signed in to change notification settings - Fork 25
/
eddsa_from.go
84 lines (62 loc) · 1.64 KB
/
eddsa_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package eddsa
import (
"crypto/rand"
"crypto/ed25519"
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
)
// 私钥
func (this EdDSA) FromPrivateKey(key []byte) EdDSA {
parsedKey, err := this.ParseEdPrivateKeyFromPEM(key)
if err != nil {
this.Error = err
return this
}
this.privateKey = parsedKey.(ed25519.PrivateKey)
return this
}
// 私钥带密码
func (this EdDSA) FromPrivateKeyWithPassword(key []byte, password string) EdDSA {
parsedKey, err := this.ParseEdPrivateKeyFromPEMWithPassword(key, password)
if err != nil {
this.Error = err
return this
}
this.privateKey = parsedKey.(ed25519.PrivateKey)
return this
}
// 公钥
func (this EdDSA) FromPublicKey(key []byte) EdDSA {
parsedKey, err := this.ParseEdPublicKeyFromPEM(key)
if err != nil {
this.Error = err
return this
}
this.publicKey = parsedKey.(ed25519.PublicKey)
return this
}
// 生成密钥
func (this EdDSA) GenerateKey() EdDSA {
this.publicKey, this.privateKey, this.Error = ed25519.GenerateKey(rand.Reader)
return this
}
// ==========
// 字节
func (this EdDSA) FromBytes(data []byte) EdDSA {
this.data = data
return this
}
// 字符
func (this EdDSA) FromString(data string) EdDSA {
this.data = []byte(data)
return this
}
// Base64
func (this EdDSA) FromBase64String(data string) EdDSA {
this.data, this.Error = cryptobin_tool.NewEncoding().Base64Decode(data)
return this
}
// Hex
func (this EdDSA) FromHexString(data string) EdDSA {
this.data, this.Error = cryptobin_tool.NewEncoding().HexDecode(data)
return this
}