MCPcopy Create free account
hub / github.com/gogs/gogs / SSHKeygenParsePublicKey

Function SSHKeygenParsePublicKey

internal/database/ssh_key.go:192–214  ·  view source on GitHub ↗

SSHKeygenParsePublicKey extracts key type and length using ssh-keygen.

(key, keyTestPath, keygenPath string)

Source from the content-addressed store, hash-verified

190
191// SSHKeygenParsePublicKey extracts key type and length using ssh-keygen.
192func SSHKeygenParsePublicKey(key, keyTestPath, keygenPath string) (string, int, error) {
193 tmpName, err := writeTmpKeyFile(key, keyTestPath)
194 if err != nil {
195 return "", 0, errors.Newf("writeTmpKeyFile: %v", err)
196 }
197 defer os.Remove(tmpName)
198
199 stdout, stderr, err := process.Exec("SSHKeygenParsePublicKey", keygenPath, "-lf", tmpName)
200 if err != nil {
201 return "", 0, errors.Newf("fail to parse public key: %s - %s", err, stderr)
202 }
203 if strings.Contains(stdout, "is not a public key file") {
204 return "", 0, ErrKeyUnableVerify{stdout}
205 }
206
207 fields := strings.Split(stdout, " ")
208 if len(fields) < 4 {
209 return "", 0, errors.Newf("invalid public key line: %s", stdout)
210 }
211
212 keyType := strings.Trim(fields[len(fields)-1], "()\r\n")
213 return strings.ToLower(keyType), com.StrTo(fields[0]).MustInt(), nil
214}
215
216// SSHNativeParsePublicKey extracts the key type and length using the golang SSH library.
217func SSHNativeParsePublicKey(keyLine string) (string, int, error) {

Callers 2

TestSSHParsePublicKeyFunction · 0.85
CheckPublicKeyStringFunction · 0.85

Calls 4

ExecFunction · 0.92
writeTmpKeyFileFunction · 0.85
RemoveMethod · 0.80
SplitMethod · 0.80

Tested by 1

TestSSHParsePublicKeyFunction · 0.68