Skip to content

Commit ea31ae8

Browse files
committed
add ecdsa and ed25519
1 parent e123054 commit ea31ae8

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

modules/ssh/ssh.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package ssh
66
import (
77
"bytes"
88
"context"
9+
"crypto/ecdsa"
10+
"crypto/ed25519"
11+
"crypto/elliptic"
912
"crypto/rand"
1013
"crypto/rsa"
1114
"crypto/x509"
@@ -58,7 +61,9 @@ const giteaPermissionExtensionKeyID = "gitea-perm-ext-key-id"
5861
type KeyType string
5962

6063
const (
61-
RSA KeyType = "rsa"
64+
RSA KeyType = "rsa"
65+
ECDSA KeyType = "ecdsa"
66+
ED25519 KeyType = "ed25519"
6267
)
6368

6469
func getExitStatusFromError(err error) int {
@@ -458,6 +463,15 @@ func keyGen(keytype KeyType) (any, any, error) {
458463
return nil, nil, err
459464
}
460465
return privateKey, &privateKey.PublicKey, nil
466+
case ED25519:
467+
pub, priv, err := ed25519.GenerateKey(rand.Reader)
468+
return priv, pub, err
469+
case ECDSA:
470+
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
471+
if err != nil {
472+
return nil, nil, err
473+
}
474+
return priv, &priv.PublicKey, nil
461475
default:
462476
return nil, nil, errors.New("unknown keyType")
463477
}

modules/ssh/ssh_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ssh_test
22

33
import (
4+
"crypto/ecdsa"
5+
"crypto/ed25519"
46
"crypto/rsa"
57
"crypto/x509"
68
"encoding/pem"
@@ -22,6 +24,14 @@ func TestGenKeyPair(t *testing.T) {
2224
keyType: ssh.RSA,
2325
expectedType: &rsa.PrivateKey{},
2426
},
27+
{
28+
keyType: ssh.ED25519,
29+
expectedType: ed25519.PrivateKey{},
30+
},
31+
{
32+
keyType: ssh.ECDSA,
33+
expectedType: &ecdsa.PrivateKey{},
34+
},
2535
}
2636
for _, tC := range testCases {
2737
t.Run("Generate"+string(tC.keyType), func(t *testing.T) {

0 commit comments

Comments
 (0)