@@ -96,17 +96,18 @@ func extractTypeFromBase64Key(key string) (string, error) {
96
96
97
97
// parseKeyString parses any key string in OpenSSH or SSH2 format to clean OpenSSH string (RFC4253).
98
98
func parseKeyString (content string ) (string , error ) {
99
- // Transform all legal line endings to a single "\n".
100
- content = strings .NewReplacer ("\r \n " , "\n " , "\r " , "\n " ).Replace (content )
101
- // remove trailing newline (and beginning spaces too)
99
+ // remove whitespace at start and end
102
100
content = strings .TrimSpace (content )
103
- lines := strings .Split (content , "\n " )
104
101
105
102
var keyType , keyContent , keyComment string
106
103
107
- if len ( lines ) == 1 {
104
+ if ! strings . Contains ( content , "-----BEGIN" ) {
108
105
// Parse OpenSSH format.
109
- parts := strings .SplitN (lines [0 ], " " , 3 )
106
+
107
+ // Remove all newlines
108
+ content = strings .NewReplacer ("\r \n " , "" , "\n " , "" ).Replace (content )
109
+
110
+ parts := strings .SplitN (content , " " , 3 )
110
111
switch len (parts ) {
111
112
case 0 :
112
113
return "" , errors .New ("empty key" )
@@ -133,6 +134,11 @@ func parseKeyString(content string) (string, error) {
133
134
}
134
135
} else {
135
136
// Parse SSH2 file format.
137
+
138
+ // Transform all legal line endings to a single "\n".
139
+ content = strings .NewReplacer ("\r \n " , "\n " , "\r " , "\n " ).Replace (content )
140
+
141
+ lines := strings .Split (content , "\n " )
136
142
continuationLine := false
137
143
138
144
for _ , line := range lines {
0 commit comments