github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/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  
    12  // KeyFingerprint returns the fingerprint and comment for the specified key
    13  // in authorized_key format. Fingerprints are generated according to RFC4716.
    14  // See ttp://www.ietf.org/rfc/rfc4716.txt, section 4.
    15  func KeyFingerprint(key string) (fingerprint, comment string, err error) {
    16  	ak, err := ParseAuthorisedKey(key)
    17  	if err != nil {
    18  		return "", "", fmt.Errorf("generating key fingerprint: %v", err)
    19  	}
    20  	hash := md5.New()
    21  	hash.Write(ak.Key)
    22  	sum := hash.Sum(nil)
    23  	var buf bytes.Buffer
    24  	for i := 0; i < hash.Size(); i++ {
    25  		if i > 0 {
    26  			buf.WriteByte(':')
    27  		}
    28  		buf.WriteString(fmt.Sprintf("%02x", sum[i]))
    29  	}
    30  	return buf.String(), ak.Comment, nil
    31  }