Skip to content

Commit

Permalink
Fix #151 using rfc4716hex
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Perez committed Jan 4, 2016
1 parent 27cb668 commit ffbdbde
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pkg/commands/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func RunInfo(ctx CommandContext, args InfoArgs) error {
fmt.Fprintln(ctx.Stdout, "")
fmt.Fprintln(ctx.Stdout, "SSH Keys:")
for id, key := range user.SSHPublicKeys {
fingerprint, err := utils.SSHGetFingerprint(key.Key)
fingerprint, err := utils.SSHGetFingerprint([]byte(key.Key))
if err != nil {
return err
}
fmt.Fprintf(ctx.Stdout, " [%d] %s", id, fingerprint)
fmt.Fprintf(ctx.Stdout, " [%d] %s\n", id, fingerprint)
}
fmt.Fprintf(ctx.Stdout, "\n")
}
Expand Down
43 changes: 25 additions & 18 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
package utils

import (
"crypto/md5"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"regexp"
"strings"
"time"

"golang.org/x/crypto/ssh"

"github.com/scaleway/scaleway-cli/pkg/sshcommand"
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
Expand Down Expand Up @@ -213,25 +216,29 @@ func AttachToSerial(serverID string, apiToken string) (*gottyclient.Client, chan
return gottycli, done, nil
}

// SSHGetFingerprint returns the fingerprint of an SSH key
func SSHGetFingerprint(key string) (string, error) {
tmp, err := ioutil.TempFile("", ".tmp")
if err != nil {
return "", fmt.Errorf("Unable to create a tempory file: %v", err)
}
defer os.Remove(tmp.Name())
buff := []byte(key)
bytesWritten := 0
for bytesWritten < len(buff) {
nb, err := tmp.Write(buff[bytesWritten:])
if err != nil {
return "", fmt.Errorf("Unable to write: %v", err)
func rfc4716hex(data []byte) string {
fingerprint := ""

for i := 0; i < len(data); i++ {
fingerprint = fmt.Sprintf("%s%0.2x", fingerprint, data[i])
if i != len(data)-1 {
fingerprint = fingerprint + ":"
}
bytesWritten += nb
}
ret, err := exec.Command("ssh-keygen", "-l", "-f", tmp.Name()).Output()
return fingerprint
}

// SSHGetFingerprint returns the fingerprint of an SSH key
func SSHGetFingerprint(key []byte) (string, error) {
publicKey, comment, _, _, err := ssh.ParseAuthorizedKey(key)
if err != nil {
return "", fmt.Errorf("Unable to run ssh-keygen: %v", err)
return "", err
}
switch reflect.TypeOf(publicKey).String() {
case "*ssh.rsaPublicKey", "*ssh.dsaPublicKey", "*ssh.ecdsaPublicKey":
md5sum := md5.Sum(publicKey.Marshal())
return publicKey.Type() + " " + rfc4716hex(md5sum[:]) + " " + comment, nil
default:
return "", errors.New("Can't handle this key")
}
return string(ret), nil
}

0 comments on commit ffbdbde

Please sign in to comment.