github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/deploykeys/parse.go (about)

     1  package deploykeys
     2  
     3  import (
     4  	"crypto/rsa"
     5  	"errors"
     6  
     7  	"golang.org/x/crypto/ssh"
     8  )
     9  
    10  func (d *SDeployKeys) ParsePrivateKey(b []byte) (*rsa.PrivateKey, error) {
    11  	in, err := ssh.ParseRawPrivateKey(b)
    12  	if err != nil {
    13  		return nil, errors.New("Invalid RSA private key format")
    14  	}
    15  	privKey := in.(*rsa.PrivateKey)
    16  	return privKey, nil
    17  }
    18  
    19  func (d *SDeployKeys) ParsePublicKey(b []byte) (ssh.PublicKey, error) {
    20  	s, _, _, _, err := ssh.ParseAuthorizedKey(b)
    21  	if err != nil {
    22  		return nil, errors.New("Invalid RSA public key format")
    23  	}
    24  	return s, nil
    25  }
    26  
    27  func (d *SDeployKeys) ExtractPublicKey(privKey *rsa.PrivateKey) (ssh.PublicKey, error) {
    28  	s, err := ssh.NewPublicKey(&privKey.PublicKey)
    29  	if err != nil {
    30  		return nil, errors.New("Invalid RSA public key format derived from private key")
    31  	}
    32  	return s, nil
    33  }