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
|
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package ssh
import (
"bytes"
"crypto/md5"
"fmt"
"github.com/juju/errors"
)
// KeyFingerprint returns the fingerprint and comment for the specified key
// in authorized_key format. Fingerprints are generated according to RFC4716.
// See ttp://www.ietf.org/rfc/rfc4716.txt, section 4.
func KeyFingerprint(key string) (fingerprint, comment string, err error) {
ak, err := ParseAuthorisedKey(key)
if err != nil {
return "", "", errors.Errorf("generating key fingerprint: %v", err)
}
hash := md5.New()
hash.Write(ak.Key)
sum := hash.Sum(nil)
var buf bytes.Buffer
for i := 0; i < hash.Size(); i++ {
if i > 0 {
buf.WriteByte(':')
}
buf.WriteString(fmt.Sprintf("%02x", sum[i]))
}
return buf.String(), ak.Comment, nil
}
|