-
Notifications
You must be signed in to change notification settings - Fork 23
/
with.go
113 lines (86 loc) · 1.98 KB
/
with.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
package dh
import (
"math/big"
"github.com/deatil/go-cryptobin/dh/dh"
)
// 设置 PrivateKey
func (this Dh) WithPrivateKey(data *dh.PrivateKey) Dh {
this.privateKey = data
return this
}
// 设置 PublicKey
func (this Dh) WithPublicKey(data *dh.PublicKey) Dh {
this.publicKey = data
return this
}
// 设置分组
func (this Dh) WithGroup(data *dh.Group) Dh {
this.group = data
return this
}
// 根据 Group 数据设置分组
func (this Dh) SetGroup(name string) Dh {
var param dh.GroupID
switch name {
case "P1001":
param = dh.P1001
case "P1002":
param = dh.P1002
case "P1536":
param = dh.P1536
case "P2048":
param = dh.P2048
case "P3072":
param = dh.P3072
case "P4096":
param = dh.P4096
case "P6144":
param = dh.P6144
case "P8192":
param = dh.P8192
default:
param = dh.P2048
}
paramGroup, err := dh.GetMODGroup(param)
if err != nil {
return this.AppendError(err)
}
this.group = paramGroup
return this
}
// 根据 Group P和G 数据设置分组
func (this Dh) SetGroupPG(p string, g int64) Dh {
pInt, _ := new(big.Int).SetString(p, 16)
this.group = &dh.Group{
P: pInt,
G: big.NewInt(g),
}
return this
}
// 随机数
func (this Dh) SetRandGroup(num int64) Dh {
hexLetters := []rune("0123456789abcdef")
// p 值
p := RandomString(num, hexLetters)
pInt, _ := new(big.Int).SetString(p, 16)
this.group = &dh.Group{
P: pInt,
G: big.NewInt(2),
}
return this
}
// 设置 keyData
func (this Dh) WithKeyData(data []byte) Dh {
this.keyData = data
return this
}
// 设置 secretData
func (this Dh) WithSecretData(data []byte) Dh {
this.secretData = data
return this
}
// 设置错误
func (this Dh) WithError(errs []error) Dh {
this.Errors = errs
return this
}