-
Notifications
You must be signed in to change notification settings - Fork 23
/
rsa_from.go
137 lines (98 loc) · 2.93 KB
/
rsa_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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package rsa
import (
"math/big"
"crypto/rsa"
"crypto/rand"
cryptobin_tool "github.com/deatil/go-cryptobin/tool"
)
// 私钥
func (this Rsa) FromPrivateKey(key []byte) Rsa {
this.privateKey, this.Error = this.ParseRSAPrivateKeyFromPEM(key)
return this
}
// 私钥带密码
func (this Rsa) FromPrivateKeyWithPassword(key []byte, password string) Rsa {
privateKey, err := this.ParseRSAPKCS8PrivateKeyFromPEMWithPassword(key, password)
if err == nil {
this.privateKey = privateKey
return this
}
this.privateKey, this.Error = this.ParseRSAPrivateKeyFromPEMWithPassword(key, password)
return this
}
// 公钥
func (this Rsa) FromPublicKey(key []byte) Rsa {
this.publicKey, this.Error = this.ParseRSAPublicKeyFromPEM(key)
return this
}
// 生成密钥
// bits = 512 | 1024 | 2048 | 4096
func (this Rsa) GenerateKey(bits int) Rsa {
this.privateKey, this.Error = rsa.GenerateKey(rand.Reader, bits)
// 生成公钥
this.publicKey = &this.privateKey.PublicKey
return this
}
// ==========
// 模数、指数生成公钥
// 指数默认为 10001
func (this Rsa) FromPublicKeyNE(nString string, e int) Rsa {
n, _ := new(big.Int).SetString(nString[:], 16)
this.publicKey = &rsa.PublicKey{
N: n,
E: e,
}
return this
}
// ==========
// Pkcs1
func (this Rsa) FromPKCS1PrivateKey(key []byte) Rsa {
this.privateKey, this.Error = this.ParseRSAPrivateKeyFromPEM(key)
return this
}
// Pkcs1WithPassword
func (this Rsa) FromPKCS1PrivateKeyWithPassword(key []byte, password string) Rsa {
this.privateKey, this.Error = this.ParseRSAPrivateKeyFromPEMWithPassword(key, password)
return this
}
// Pkcs8
func (this Rsa) FromPKCS8PrivateKey(key []byte) Rsa {
this.privateKey, this.Error = this.ParseRSAPrivateKeyFromPEM(key)
return this
}
// Pkcs8WithPassword
func (this Rsa) FromPKCS8PrivateKeyWithPassword(key []byte, password string) Rsa {
this.privateKey, this.Error = this.ParseRSAPKCS8PrivateKeyFromPEMWithPassword(key, password)
return this
}
// Pkcs12 Cert
func (this Rsa) FromPKCS12Cert(key []byte) Rsa {
this.privateKey, this.Error = this.ParseRSAPKCS12CertFromPEMWithPassword(key, "")
return this
}
// Pkcs12CertWithPassword
func (this Rsa) FromPKCS12CertWithPassword(key []byte, password string) Rsa {
this.privateKey, this.Error = this.ParseRSAPKCS12CertFromPEMWithPassword(key, password)
return this
}
// ==========
// 字节
func (this Rsa) FromBytes(data []byte) Rsa {
this.data = data
return this
}
// 字符
func (this Rsa) FromString(data string) Rsa {
this.data = []byte(data)
return this
}
// Base64
func (this Rsa) FromBase64String(data string) Rsa {
this.data, this.Error = cryptobin_tool.NewEncoding().Base64Decode(data)
return this
}
// Hex
func (this Rsa) FromHexString(data string) Rsa {
this.data, this.Error = cryptobin_tool.NewEncoding().HexDecode(data)
return this
}