I'm using this method to extract and get the public key string from an 
original ssh private key. They key ends up to be broken/badly formatted. 
I've checked it against the key generated by ssh-keygen and the first few 
chars are different. Anyone has any ideas?

func ExtractPublicKey(der []byte) (*string, error) {
       var privateKey crypto.PrivateKey
       var publicKey crypto.PublicKey
       var ok bool = true
       var err error

       privateKey, err = ssh.ParseRawPrivateKey([]byte(der))

       if err != nil {
              return nil, err
       }
       switch privateKey.(type) {
       case *rsa.PrivateKey:
              fmt.Println("*rsa.PrivateKey")
              key := privateKey.(*rsa.PrivateKey)
              publicKey = key.PublicKey
       case *ecdsa.PrivateKey:
              fmt.Println("*ecdsa.PrivateKey")
              key := privateKey.(*ecdsa.PrivateKey)
              publicKey = key.Public()
       case *dsa.PrivateKey:
              fmt.Println("*dsa.PrivateKey")
              key := privateKey.(*dsa.PrivateKey)
              publicKey = key.PublicKey
       case *ed25519.PrivateKey:
              fmt.Println("*ed25519.PrivateKey")
              key := privateKey.(*ed25519.PrivateKey)
              publicKey = key.Public()
       default:
              return nil, errors.New("Failed to defined key type")
       }
       if !ok {
              return nil, fmt.Errorf("Failed to convert key to %s", 
"rsa.PrivateKey")
       }
       fmt.Println(string(ssh.Marshal(publicKey)))

       str := base64.StdEncoding.EncodeToString(ssh.Marshal(publicKey))
       return &str, nil
}



-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to