This commit is contained in:
nemunaire 2018-02-08 23:26:29 +01:00
parent 404792c2eb
commit 7d681aa95a
1 changed files with 41 additions and 0 deletions

41
validator/ssh.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"bytes"
"fmt"
"os/exec"
"regexp"
"strings"
)
type SSHkey struct {
Size int
Fingerprint string
Comment string
Algo string
}
func SSHkeyAnalyse(key string) (s SSHkey, err error) {
cmd := exec.Command("ssh-keygen", "-l", "-f", "-")
cmd.Stdin = strings.NewReader(key)
var out bytes.Buffer
cmd.Stdout = &out
if err = cmd.Run(); err != nil {
return
}
var validLine = regexp.MustCompile(`^([0-9]+)`)
for _, line := range strings.Split(out.String(), "\n") {
if validLine.MatchString(line) {
s.Size, err = fmt.Sscanf("%d", validLine.SubexpNames()[1])
s.Fingerprint = validLine.SubexpNames()[2]
s.Comment = validLine.SubexpNames()[3]
s.Algo = validLine.SubexpNames()[4]
}
}
return
}