/
sshkeys.go
56 lines (47 loc) · 895 Bytes
/
sshkeys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package sshkeys
import (
"crypto/md5"
"fmt"
"golang.org/x/crypto/ssh"
)
type SSHKey struct {
Key []byte
Pubkey ssh.PublicKey
Comment string
Options []string
Rest []byte
Type string
}
func NewSSHKey(key []byte) (*SSHKey, error) {
sshkey := SSHKey{
Key: key,
}
err := sshkey.parse()
if err != nil {
return nil, err
}
return &sshkey, nil
}
func (s *SSHKey) parse() error {
var err error
s.Pubkey, s.Comment, s.Options, s.Rest, err = ssh.ParseAuthorizedKey(s.Key)
if err != nil {
return err
}
s.Type = s.Pubkey.Type()
// FIXME: compute bits
// FIXME: check min/max key length
return nil
}
func (s *SSHKey) Hash() string {
output := ""
hash := fmt.Sprintf("%x", md5.Sum(s.Pubkey.Marshal()))
hashLength := len(hash)
for i, r := range []rune(hash) {
output += string(r)
if (i+1)%2 == 0 && i < hashLength-1 {
output += ":"
}
}
return output
}