launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/utils/ssh/fingerprint.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package ssh 5 6 import ( 7 "bytes" 8 "crypto/md5" 9 "fmt" 10 11 "launchpad.net/errgo/errors" 12 ) 13 14 // KeyFingerprint returns the fingerprint and comment for the specified key 15 // in authorized_key format. Fingerprints are generated according to RFC4716. 16 // See ttp://www.ietf.org/rfc/rfc4716.txt, section 4. 17 func KeyFingerprint(key string) (fingerprint, comment string, err error) { 18 ak, err := ParseAuthorisedKey(key) 19 if err != nil { 20 return "", "", errors.Notef(err, "generating key fingerprint") 21 } 22 hash := md5.New() 23 hash.Write(ak.Key) 24 sum := hash.Sum(nil) 25 var buf bytes.Buffer 26 for i := 0; i < hash.Size(); i++ { 27 if i > 0 { 28 buf.WriteByte(':') 29 } 30 buf.WriteString(fmt.Sprintf("%02x", sum[i])) 31 } 32 return buf.String(), ak.Comment, nil 33 }