@@ -13,35 +13,33 @@ import (
13
13
"golang.org/x/xerrors"
14
14
)
15
15
16
- type SSHKeygenAlgorithm string
16
+ type Algorithm string
17
17
18
18
const (
19
- // SSHKeygenAlgorithmEd25519 is the Edwards-curve Digital Signature Algorithm using Curve25519
20
- SSHKeygenAlgorithmEd25519 SSHKeygenAlgorithm = "ed25519"
21
- // SSHKeygenAlgorithmECDSA is the Digital Signature Algorithm (DSA) using NIST Elliptic Curve
22
- SSHKeygenAlgorithmECDSA SSHKeygenAlgorithm = "ecdsa"
23
- // SSHKeygenAlgorithmRSA4096 is the venerable Rivest-Shamir-Adleman algorithm
19
+ // AlgorithmEd25519 is the Edwards-curve Digital Signature Algorithm using Curve25519
20
+ AlgorithmEd25519 Algorithm = "ed25519"
21
+ // AlgorithmECDSA is the Digital Signature Algorithm (DSA) using NIST Elliptic Curve
22
+ AlgorithmECDSA Algorithm = "ecdsa"
23
+ // AlgorithmRSA4096 is the venerable Rivest-Shamir-Adleman algorithm
24
24
// and creates a key with a fixed size of 4096-bit.
25
- SSHKeygenAlgorithmRSA4096 SSHKeygenAlgorithm = "rsa4096"
25
+ AlgorithmRSA4096 Algorithm = "rsa4096"
26
26
)
27
27
28
- func GenerateKeyPair (algo SSHKeygenAlgorithm ) ([]byte , []byte , error ) {
28
+ func GenerateKeyPair (algo Algorithm ) ([]byte , []byte , error ) {
29
29
switch algo {
30
- case SSHKeygenAlgorithmEd25519 :
30
+ case AlgorithmEd25519 :
31
31
return ed25519KeyGen ()
32
- case SSHKeygenAlgorithmECDSA :
32
+ case AlgorithmECDSA :
33
33
return ecdsaKeyGen ()
34
- case SSHKeygenAlgorithmRSA4096 :
34
+ case AlgorithmRSA4096 :
35
35
return rsa4096KeyGen ()
36
36
default :
37
- return nil , nil , xerrors .Errorf ("invalid SSHKeygenAlgorithm : %s" , algo )
37
+ return nil , nil , xerrors .Errorf ("invalid algorithm : %s" , algo )
38
38
}
39
39
}
40
40
41
41
// ed25519KeyGen returns an ED25519-based SSH private key.
42
42
func ed25519KeyGen () ([]byte , []byte , error ) {
43
- const blockType = "OPENSSH PRIVATE KEY"
44
-
45
43
publicKey , privateKeyRaw , err := ed25519 .GenerateKey (rand .Reader )
46
44
if err != nil {
47
45
return nil , nil , xerrors .Errorf ("generate ed25519 private key: %w" , err )
@@ -56,7 +54,7 @@ func ed25519KeyGen() ([]byte, []byte, error) {
56
54
}
57
55
58
56
pb := pem.Block {
59
- Type : blockType ,
57
+ Type : "OPENSSH PRIVATE KEY" ,
60
58
Headers : nil ,
61
59
Bytes : byt ,
62
60
}
@@ -67,15 +65,13 @@ func ed25519KeyGen() ([]byte, []byte, error) {
67
65
68
66
// ecdsaKeyGen returns an ECDSA-based SSH private key.
69
67
func ecdsaKeyGen () ([]byte , []byte , error ) {
70
- const blockType = "EC PRIVATE KEY"
71
-
72
68
privateKeyRaw , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
73
69
if err != nil {
74
70
return nil , nil , xerrors .Errorf ("generate ecdsa private key: %w" , err )
75
71
}
76
72
publicKey , err := x509 .MarshalPKIXPublicKey (privateKeyRaw .PublicKey )
77
73
if err != nil {
78
- return nil , nil , xerrors .Errorf ("generate RSA4096 public key: %w" , err )
74
+ return nil , nil , xerrors .Errorf ("generate ecdsa public key: %w" , err )
79
75
}
80
76
81
77
byt , err := x509 .MarshalECPrivateKey (privateKeyRaw )
@@ -84,7 +80,7 @@ func ecdsaKeyGen() ([]byte, []byte, error) {
84
80
}
85
81
86
82
pb := pem.Block {
87
- Type : blockType ,
83
+ Type : "EC PRIVATE KEY" ,
88
84
Headers : nil ,
89
85
Bytes : byt ,
90
86
}
@@ -97,8 +93,6 @@ func ecdsaKeyGen() ([]byte, []byte, error) {
97
93
//
98
94
// Administrators may configure this for SSH key compatibility with Azure DevOps.
99
95
func rsa4096KeyGen () ([]byte , []byte , error ) {
100
- const blockType = "RSA PRIVATE KEY"
101
-
102
96
privateKeyRaw , err := rsa .GenerateKey (rand .Reader , 4096 )
103
97
if err != nil {
104
98
return nil , nil , xerrors .Errorf ("generate RSA4096 private key: %w" , err )
@@ -109,7 +103,7 @@ func rsa4096KeyGen() ([]byte, []byte, error) {
109
103
}
110
104
111
105
pb := pem.Block {
112
- Type : blockType ,
106
+ Type : "RSA PRIVATE KEY" ,
113
107
Bytes : x509 .MarshalPKCS1PrivateKey (privateKeyRaw ),
114
108
}
115
109
privateKey := pem .EncodeToMemory (& pb )
@@ -118,16 +112,16 @@ func rsa4096KeyGen() ([]byte, []byte, error) {
118
112
}
119
113
120
114
// ParseSSHKeygenAlgorithm returns a valid SSHKeygenAlgorithm or error if input is not a valid.
121
- func ParseSSHKeygenAlgorithm (t string ) (SSHKeygenAlgorithm , error ) {
115
+ func ParseSSHKeygenAlgorithm (t string ) (Algorithm , error ) {
122
116
ok := []string {
123
- string (SSHKeygenAlgorithmEd25519 ),
124
- string (SSHKeygenAlgorithmECDSA ),
125
- string (SSHKeygenAlgorithmRSA4096 ),
117
+ string (AlgorithmEd25519 ),
118
+ string (AlgorithmECDSA ),
119
+ string (AlgorithmRSA4096 ),
126
120
}
127
121
128
122
for _ , a := range ok {
129
- if string ( t ) == a {
130
- return SSHKeygenAlgorithm (a ), nil
123
+ if t == a {
124
+ return Algorithm (a ), nil
131
125
}
132
126
}
133
127
0 commit comments