Skip to content

Commit dea3ad8

Browse files
committed
优化
1 parent 3395066 commit dea3ad8

File tree

9 files changed

+92
-8
lines changed

9 files changed

+92
-8
lines changed

cryptobin/ssh/create.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ func (this SSH) CreateOpenSSHPrivateKeyWithPassword(password []byte, opts ...Ope
212212
}
213213

214214
useOpts := DefaultOpenSSHOpts
215+
if this.options.CipherName != "" {
216+
cip, err := ssh.ParseCipher(this.options.CipherName)
217+
if err != nil {
218+
return this.AppendError(err)
219+
}
220+
221+
useOpts.Cipher = cip
222+
}
223+
215224
if len(opts) > 0 {
216225
useOpts = opts[0]
217226
}

cryptobin/ssh/from.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,14 @@ func FromPublicKey(key []byte) SSH {
143143

144144
// from OpenSSH PrivateKey
145145
func (this SSH) FromOpenSSHPrivateKey(key []byte) SSH {
146-
privateKey, comment, err := this.ParseOpenSSHPrivateKeyFromPEM(key)
146+
privateKey, comment, cipherName, err := this.ParseOpenSSHPrivateKeyFromPEM(key)
147147
if err != nil {
148148
return this.AppendError(err)
149149
}
150150

151151
this.privateKey = privateKey
152152
this.options.Comment = comment
153+
this.options.CipherName = cipherName
153154

154155
return this
155156
}
@@ -161,13 +162,14 @@ func FromOpenSSHPrivateKey(key []byte) SSH {
161162

162163
// from OpenSSH PrivateKey with password
163164
func (this SSH) FromOpenSSHPrivateKeyWithPassword(key []byte, password []byte) SSH {
164-
privateKey, comment, err := this.ParseOpenSSHPrivateKeyFromPEMWithPassword(key, password)
165+
privateKey, comment, cipherName, err := this.ParseOpenSSHPrivateKeyFromPEMWithPassword(key, password)
165166
if err != nil {
166167
return this.AppendError(err)
167168
}
168169

169170
this.privateKey = privateKey
170171
this.options.Comment = comment
172+
this.options.CipherName = cipherName
171173

172174
return this
173175
}

cryptobin/ssh/get.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ func (this SSH) GetOptions() Options {
7575
return this.options
7676
}
7777

78+
// get Options CipherName
79+
func (this SSH) GetCipherName() string {
80+
return this.options.CipherName
81+
}
82+
7883
// get Options Comment
7984
func (this SSH) GetComment() string {
8085
return this.options.Comment

cryptobin/ssh/parse.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,44 @@ var (
3333
)
3434

3535
// Parse OpenSSH PrivateKey From PEM
36-
func (this SSH) ParseOpenSSHPrivateKeyFromPEM(key []byte) (crypto.PrivateKey, string, error) {
36+
func (this SSH) ParseOpenSSHPrivateKeyFromPEM(key []byte) (crypto.PrivateKey, string, string, error) {
3737
// Parse PEM block
3838
var block *pem.Block
3939
if block, _ = pem.Decode(key); block == nil {
40-
return nil, "", ErrKeyMustBePEMEncoded
40+
return nil, "", "", ErrKeyMustBePEMEncoded
4141
}
4242

43-
return ssh.ParseOpenSSHPrivateKey(block.Bytes)
43+
privateKey, comment, err := ssh.ParseOpenSSHPrivateKey(block.Bytes)
44+
if err != nil {
45+
return nil, "", "", err
46+
}
47+
48+
info, err := ssh.ParseOpenSSHPrivateKeyToInfo(block.Bytes)
49+
if err != nil {
50+
return nil, "", "", err
51+
}
52+
53+
return privateKey, comment, info.CipherName, nil
4454
}
4555

4656
// Parse OpenSSH PrivateKey From PEM With Password
47-
func (this SSH) ParseOpenSSHPrivateKeyFromPEMWithPassword(key []byte, password []byte) (crypto.PrivateKey, string, error) {
57+
func (this SSH) ParseOpenSSHPrivateKeyFromPEMWithPassword(key []byte, password []byte) (crypto.PrivateKey, string, string, error) {
4858
var block *pem.Block
4959
if block, _ = pem.Decode(key); block == nil {
50-
return nil, "", ErrKeyMustBePEMEncoded
60+
return nil, "", "", ErrKeyMustBePEMEncoded
61+
}
62+
63+
privateKey, comment, err := ssh.ParseOpenSSHPrivateKeyWithPassword(block.Bytes, password)
64+
if err != nil {
65+
return nil, "", "", err
66+
}
67+
68+
info, err := ssh.ParseOpenSSHPrivateKeyToInfo(block.Bytes)
69+
if err != nil {
70+
return nil, "", "", err
5171
}
5272

53-
return ssh.ParseOpenSSHPrivateKeyWithPassword(block.Bytes, password)
73+
return privateKey, comment, info.CipherName, nil
5474
}
5575

5676
// Parse OpenSSH PublicKey From PEM

cryptobin/ssh/ssh.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type Options struct {
4343
// public key type
4444
PublicKeyType PublicKeyType
4545

46+
// Cipher Name
47+
CipherName string
48+
4649
// comment data
4750
Comment string
4851

cryptobin/ssh/ssh_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"crypto/rand"
99
"crypto/elliptic"
1010

11+
cryptobin_ssh "github.com/deatil/go-cryptobin/ssh"
1112
cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
1213
)
1314

@@ -341,6 +342,7 @@ func Test_Get(t *testing.T) {
341342
testerr := errors.New("test-error")
342343
opts := Options{
343344
PublicKeyType: KeyTypeRSA,
345+
CipherName: "test-CipherName",
344346
Comment: "test-Comment",
345347
ParameterSizes: dsa.L1024N160,
346348
Curve: elliptic.P256(),
@@ -373,6 +375,7 @@ func Test_Get(t *testing.T) {
373375
assertNotEmpty(openSSHPublicKey, "Test_Get-GetOpenSSHPublicKey")
374376

375377
assertEqual(newSSH2.GetOptions(), opts, "Test_Get-GetOptions")
378+
assertEqual(newSSH2.GetCipherName(), "test-CipherName", "Test_Get-GetCipherName")
376379
assertEqual(newSSH2.GetComment(), "test-Comment", "Test_Get-GetComment")
377380
assertEqual(newSSH2.GetParameterSizes(), dsa.L1024N160, "Test_Get-GetParameterSizes")
378381
assertEqual(newSSH2.GetCurve(), elliptic.P256(), "Test_Get-GetCurve")
@@ -430,6 +433,12 @@ func Test_With(t *testing.T) {
430433
tmp = newSSH.SetPublicKeyType("ECDSA")
431434
assertEqual(tmp.options.PublicKeyType, KeyTypeECDSA, "Test_Get-SetPublicKeyType")
432435

436+
tmp = newSSH.WithCipherName("test-CipherName")
437+
assertEqual(tmp.options.CipherName, "test-CipherName", "Test_Get-WithCipherName")
438+
439+
tmp = newSSH.SetCipher(cryptobin_ssh.AES256CBC)
440+
assertEqual(tmp.options.CipherName, "aes256-cbc", "Test_Get-SetCipher")
441+
433442
tmp = newSSH.WithComment("test-Comment")
434443
assertEqual(tmp.options.Comment, "test-Comment", "Test_Get-WithComment")
435444

cryptobin/ssh/with.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ func (this SSH) SetPublicKeyType(keyType string) SSH {
7777
return this
7878
}
7979

80+
// With CipherName
81+
func (this SSH) WithCipherName(cipherName string) SSH {
82+
this.options.CipherName = cipherName
83+
84+
return this
85+
}
86+
87+
// Set Cipher
88+
func (this SSH) SetCipher(cip cryptobin_ssh.Cipher) SSH {
89+
this.options.CipherName = cip.Name()
90+
91+
return this
92+
}
93+
8094
// With Comment
8195
func (this SSH) WithComment(comment string) SSH {
8296
this.options.Comment = comment

ssh/ssh.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,21 @@ func ParseOpenSSHPrivateKeyWithPassword(key []byte, password []byte) (crypto.Pri
119119
return parsedKey, comment, nil
120120
}
121121

122+
func ParseOpenSSHPrivateKeyToInfo(key []byte) (openSSHPrivateKey, error) {
123+
if len(key) < len(sshMagic) || string(key[:len(sshMagic)]) != sshMagic {
124+
return openSSHPrivateKey{}, errors.New("invalid openssh private key format")
125+
}
126+
127+
remaining := key[len(sshMagic):]
128+
129+
var w openSSHPrivateKey
130+
if err := ssh.Unmarshal(remaining, &w); err != nil {
131+
return openSSHPrivateKey{}, err
132+
}
133+
134+
return w, nil
135+
}
136+
122137
// Marshal OpenSSH PrivateKey
123138
func MarshalOpenSSHPrivateKey(rand io.Reader, key crypto.PrivateKey, comment string) (*pem.Block, error) {
124139
return MarshalOpenSSHPrivateKeyWithPassword(rand, key, comment, nil)

ssh/ssh_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ func Test_ParseSSHKey_Ecdsa_With_Pass(t *testing.T) {
216216
assertNotEmpty(sshComment, "Test_ParseSSHKey_Ecdsa_With_Pass-commit")
217217

218218
assertEqual(sshComment, "test-ssh123", "Test_ParseSSHKey_Ecdsa_With_Pass")
219+
220+
block2, _ := pem.Decode(blockkeyData)
221+
222+
w, err := ParseOpenSSHPrivateKeyToInfo(block2.Bytes)
223+
assertError(err, "Test_ParseSSHKey_DSA-ParseOpenSSHPrivateKeyToInfo")
224+
assertEqual(w.CipherName, "aes256-ctr", "Test_ParseSSHKey_DSA-CipherName")
225+
assertEqual(w.KdfName, "bcrypt", "Test_ParseSSHKey_DSA-KdfName")
219226
}
220227

221228
func test_ParseSSHKey_Ecdsa_With_Pass_And_Opts(t *testing.T, opts Opts) {

0 commit comments

Comments
 (0)