Skip to content

Commit ef4e256

Browse files
committed
优化 OPENSSH
1 parent 348d5e8 commit ef4e256

File tree

19 files changed

+1554
-4
lines changed

19 files changed

+1554
-4
lines changed

cryptobin/ssh/check.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ecgdsa
2+
3+
import (
4+
"crypto"
5+
"reflect"
6+
)
7+
8+
type publicKeyEqual interface {
9+
Equal(x crypto.PublicKey) bool
10+
}
11+
12+
// 检测公钥私钥是否匹配
13+
func (this SSH) CheckKeyPair() bool {
14+
// 私钥导出的公钥
15+
pubKeyFromPriKey := this.MakePublicKey().publicKey
16+
17+
if pubKeyFromPriKey == nil || this.publicKey == nil {
18+
return false
19+
}
20+
21+
if pubkeyEqual, ok := pubKeyFromPriKey.(publicKeyEqual); ok {
22+
if pubkeyEqual.Equal(this.publicKey) {
23+
return true
24+
}
25+
}
26+
27+
if reflect.DeepEqual(pubKeyFromPriKey, this.publicKey) {
28+
return true
29+
}
30+
31+
return false
32+
}

cryptobin/ssh/create.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package ecgdsa
2+
3+
import (
4+
"errors"
5+
"crypto/rand"
6+
"encoding/pem"
7+
8+
"github.com/deatil/go-cryptobin/ssh"
9+
)
10+
11+
type (
12+
// 配置
13+
Opts = ssh.Opts
14+
)
15+
16+
var (
17+
// get Cipher
18+
GetCipherFromName = ssh.GetCipherFromName
19+
20+
// Default options
21+
DefaultOpts = ssh.DefaultOpts
22+
)
23+
24+
// 生成私钥 pem 数据
25+
func (this SSH) CreatePrivateKey() SSH {
26+
return this.CreateOpensshPrivateKey()
27+
}
28+
29+
// 生成私钥带密码 pem 数据, PKCS1 别名
30+
func (this SSH) CreatePrivateKeyWithPassword(password []byte, opts ...Opts) SSH {
31+
return this.CreateOpensshPrivateKeyWithPassword(password, opts...)
32+
}
33+
34+
// 生成公钥 pem 数据
35+
func (this SSH) CreatePublicKey() SSH {
36+
return this.CreateOpensshPublicKey()
37+
}
38+
39+
// ====================
40+
41+
// 生成私钥 pem 数据
42+
func (this SSH) CreateOpensshPrivateKey() SSH {
43+
if this.privateKey == nil {
44+
err := errors.New("privateKey empty.")
45+
return this.AppendError(err)
46+
}
47+
48+
privateBlock, err := ssh.MarshalOpenSSHPrivateKey(
49+
rand.Reader,
50+
this.privateKey,
51+
this.options.Comment,
52+
)
53+
if err != nil {
54+
return this.AppendError(err)
55+
}
56+
57+
this.keyData = pem.EncodeToMemory(privateBlock)
58+
59+
return this
60+
}
61+
62+
// 生成私钥带密码 pem 数据
63+
func (this SSH) CreateOpensshPrivateKeyWithPassword(password []byte, opts ...Opts) SSH {
64+
if this.privateKey == nil {
65+
err := errors.New("privateKey empty.")
66+
return this.AppendError(err)
67+
}
68+
69+
useOpts := DefaultOpts
70+
if len(opts) > 0 {
71+
useOpts = opts[0]
72+
}
73+
74+
// 生成私钥
75+
privateBlock, err := ssh.MarshalOpenSSHPrivateKeyWithPassword(
76+
rand.Reader,
77+
this.privateKey,
78+
this.options.Comment,
79+
password,
80+
useOpts,
81+
)
82+
if err != nil {
83+
return this.AppendError(err)
84+
}
85+
86+
this.keyData = pem.EncodeToMemory(privateBlock)
87+
88+
return this
89+
}
90+
91+
// 生成公钥 pem 数据
92+
func (this SSH) CreateOpensshPublicKey() SSH {
93+
if this.publicKey == nil {
94+
err := errors.New("publicKey empty.")
95+
return this.AppendError(err)
96+
}
97+
98+
sshPublicKey, err := ssh.NewPublicKey(this.publicKey)
99+
if err != nil {
100+
return this.AppendError(err)
101+
}
102+
103+
if this.options.Comment != "" {
104+
this.keyData = ssh.MarshalAuthorizedKeyWithComment(sshPublicKey, this.options.Comment)
105+
} else {
106+
this.keyData = ssh.MarshalAuthorizedKey(sshPublicKey)
107+
}
108+
109+
return this
110+
}

cryptobin/ssh/error.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ecgdsa
2+
3+
import (
4+
"github.com/deatil/go-cryptobin/tool/errors"
5+
)
6+
7+
// 添加错误
8+
func (this SSH) AppendError(err ...error) SSH {
9+
this.Errors = append(this.Errors, err...)
10+
11+
return this
12+
}
13+
14+
// 获取错误
15+
func (this SSH) Error() error {
16+
return errors.Join(this.Errors...)
17+
}

cryptobin/ssh/from.go

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
package ecgdsa
2+
3+
import (
4+
"io"
5+
"crypto/rand"
6+
"crypto/rsa"
7+
"crypto/dsa"
8+
"crypto/ecdsa"
9+
"crypto/ed25519"
10+
11+
"github.com/deatil/go-cryptobin/gm/sm2"
12+
"github.com/deatil/go-cryptobin/tool/encoding"
13+
)
14+
15+
// 生成密钥
16+
func (this SSH) GenerateKeyWithSeed(reader io.Reader) SSH {
17+
switch this.options.PublicKeyType {
18+
case KeyTypeRSA:
19+
privateKey, err := rsa.GenerateKey(reader, this.options.Bits)
20+
if err != nil {
21+
return this.AppendError(err)
22+
}
23+
24+
this.privateKey = privateKey
25+
this.publicKey = &privateKey.PublicKey
26+
case KeyTypeDSA:
27+
privateKey := &dsa.PrivateKey{}
28+
dsa.GenerateParameters(&privateKey.Parameters, reader, this.options.ParameterSizes)
29+
dsa.GenerateKey(privateKey, reader)
30+
31+
this.privateKey = privateKey
32+
this.publicKey = &privateKey.PublicKey
33+
case KeyTypeECDSA:
34+
privateKey, err := ecdsa.GenerateKey(this.options.Curve, reader)
35+
if err != nil {
36+
return this.AppendError(err)
37+
}
38+
39+
this.privateKey = privateKey
40+
this.publicKey = &privateKey.PublicKey
41+
case KeyTypeEdDSA:
42+
publicKey, privateKey, err := ed25519.GenerateKey(reader)
43+
if err != nil {
44+
return this.AppendError(err)
45+
}
46+
47+
this.privateKey = privateKey
48+
this.publicKey = publicKey
49+
case KeyTypeSM2:
50+
privateKey, err := sm2.GenerateKey(reader)
51+
if err != nil {
52+
return this.AppendError(err)
53+
}
54+
55+
this.privateKey = privateKey
56+
this.publicKey = &privateKey.PublicKey
57+
}
58+
59+
return this
60+
}
61+
62+
// 使用自定义数据生成密钥对
63+
func GenerateKeyWithSeed(reader io.Reader, options Options) SSH {
64+
return defaultSSH.
65+
WithOptions(options).
66+
GenerateKeyWithSeed(reader)
67+
}
68+
69+
// 生成密钥
70+
func (this SSH) GenerateKey() SSH {
71+
return this.GenerateKeyWithSeed(rand.Reader)
72+
}
73+
74+
// 生成密钥对
75+
func GenerateKey(options Options) SSH {
76+
return defaultSSH.
77+
WithOptions(options).
78+
GenerateKey()
79+
}
80+
81+
// ==========
82+
83+
// 私钥
84+
func (this SSH) FromPrivateKey(key []byte) SSH {
85+
return this.FromOpensshPrivateKey(key)
86+
}
87+
88+
// 私钥
89+
func FromPrivateKey(key []byte) SSH {
90+
return defaultSSH.FromPrivateKey(key)
91+
}
92+
93+
// 私钥带密码
94+
func (this SSH) FromPrivateKeyWithPassword(key []byte, password []byte) SSH {
95+
return this.FromOpensshPrivateKeyWithPassword(key, password)
96+
}
97+
98+
// 私钥带密码
99+
func FromPrivateKeyWithPassword(key []byte, password []byte) SSH {
100+
return defaultSSH.FromPrivateKeyWithPassword(key, password)
101+
}
102+
103+
// 公钥
104+
func (this SSH) FromPublicKey(key []byte) SSH {
105+
return defaultSSH.FromOpensshPublicKey(key)
106+
}
107+
108+
// 公钥
109+
func FromPublicKey(key []byte) SSH {
110+
return defaultSSH.FromPublicKey(key)
111+
}
112+
113+
// ==========
114+
115+
// 私钥
116+
func (this SSH) FromOpensshPrivateKey(key []byte) SSH {
117+
privateKey, comment, err := this.ParseOpensshPrivateKeyFromPEM(key)
118+
if err != nil {
119+
return this.AppendError(err)
120+
}
121+
122+
this.privateKey = privateKey
123+
this.options.Comment = comment
124+
125+
return this
126+
}
127+
128+
// 私钥
129+
func FromOpensshPrivateKey(key []byte) SSH {
130+
return defaultSSH.FromOpensshPrivateKey(key)
131+
}
132+
133+
// 私钥带密码
134+
func (this SSH) FromOpensshPrivateKeyWithPassword(key []byte, password []byte) SSH {
135+
privateKey, comment, err := this.ParseOpensshPrivateKeyFromPEMWithPassword(key, password)
136+
if err != nil {
137+
return this.AppendError(err)
138+
}
139+
140+
this.privateKey = privateKey
141+
this.options.Comment = comment
142+
143+
return this
144+
}
145+
146+
// 私钥
147+
func FromOpensshPrivateKeyWithPassword(key []byte, password []byte) SSH {
148+
return defaultSSH.FromOpensshPrivateKeyWithPassword(key, password)
149+
}
150+
151+
// 公钥
152+
func (this SSH) FromOpensshPublicKey(key []byte) SSH {
153+
publicKey, comment, err := this.ParseOpensshPublicKeyFromPEM(key)
154+
if err != nil {
155+
return this.AppendError(err)
156+
}
157+
158+
this.publicKey = publicKey
159+
this.options.Comment = comment
160+
161+
return this
162+
}
163+
164+
// 公钥
165+
func FromOpensshPublicKey(key []byte) SSH {
166+
return defaultSSH.FromOpensshPublicKey(key)
167+
}
168+
169+
// ==========
170+
171+
// 字节
172+
func (this SSH) FromBytes(data []byte) SSH {
173+
this.data = data
174+
175+
return this
176+
}
177+
178+
// 字节
179+
func FromBytes(data []byte) SSH {
180+
return defaultSSH.FromBytes(data)
181+
}
182+
183+
// 字符
184+
func (this SSH) FromString(data string) SSH {
185+
this.data = []byte(data)
186+
187+
return this
188+
}
189+
190+
// 字符
191+
func FromString(data string) SSH {
192+
return defaultSSH.FromString(data)
193+
}
194+
195+
// Base64
196+
func (this SSH) FromBase64String(data string) SSH {
197+
newData, err := encoding.Base64Decode(data)
198+
199+
this.data = newData
200+
201+
return this.AppendError(err)
202+
}
203+
204+
// Base64
205+
func FromBase64String(data string) SSH {
206+
return defaultSSH.FromBase64String(data)
207+
}
208+
209+
// Hex
210+
func (this SSH) FromHexString(data string) SSH {
211+
newData, err := encoding.HexDecode(data)
212+
213+
this.data = newData
214+
215+
return this.AppendError(err)
216+
}
217+
218+
// Hex
219+
func FromHexString(data string) SSH {
220+
return defaultSSH.FromHexString(data)
221+
}

0 commit comments

Comments
 (0)