Golang Generate Rsa Key Pem Public
Golang Generate Rsa Key Pem Public 4,6/5 2474 reviews
Key
Generate SSH RSA Private/Public Key pair with Golang

$ openssl genrsa -out private.pem 2048 Extract public key( public.pem ) from private.pem with the following command. $ openssl rsa -in private.pem -outform PEM -pubout -out public.pem.

Generate public ssh key aix. More information on SSH keys can be found.You can generate an SSH key pair directly in cPanel, or you can generate the keys yourself and just upload the public one in cPanel to use with your hosting account.When generating SSH keys yourself under Linux, you can use the ssh-keygen command. Open up the Terminal. To do so follow these steps:.

gistfile1.txt
// This shows an example of how to generate a SSH RSA Private/Public key pair and save it locally
package main
import (
'crypto/rand'
'crypto/rsa'
'crypto/x509'
'encoding/pem'
'golang.org/x/crypto/ssh'
'io/ioutil'
'log'
)
func main() {
savePrivateFileTo := './id_rsa_test'
savePublicFileTo := './id_rsa_test.pub'
bitSize := 4096
privateKey, err := generatePrivateKey(bitSize)
if err != nil {
log.Fatal(err.Error())
}
publicKeyBytes, err := generatePublicKey(&privateKey.PublicKey)
if err != nil {
log.Fatal(err.Error())
}
privateKeyBytes := encodePrivateKeyToPEM(privateKey)
err = writeKeyToFile(privateKeyBytes, savePrivateFileTo)
if err != nil {
log.Fatal(err.Error())
}
err = writeKeyToFile([]byte(publicKeyBytes), savePublicFileTo)
if err != nil {
log.Fatal(err.Error())
}
}
// generatePrivateKey creates a RSA Private Key of specified byte size
func generatePrivateKey(bitSize int) (*rsa.PrivateKey, error) {
// Private Key generation
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
return nil, err
}
// Validate Private Key
err = privateKey.Validate()
if err != nil {
return nil, err
}
log.Println('Private Key generated')
return privateKey, nil
}
// encodePrivateKeyToPEM encodes Private Key from RSA to PEM format
func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte {
// Get ASN.1 DER format
privDER := x509.MarshalPKCS1PrivateKey(privateKey)
// pem.Block
privBlock := pem.Block{
Type: 'RSA PRIVATE KEY',
Headers: nil,
Bytes: privDER,
}
// Private key in PEM format
privatePEM := pem.EncodeToMemory(&privBlock)
return privatePEM
}
// generatePublicKey take a rsa.PublicKey and return bytes suitable for writing to .pub file
// returns in the format 'ssh-rsa ..'
func generatePublicKey(privatekey *rsa.PublicKey) ([]byte, error) {
publicRsaKey, err := ssh.NewPublicKey(privatekey)
if err != nil {
return nil, err
}
pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey)
log.Println('Public key generated')
return pubKeyBytes, nil
}
// writePemToFile writes keys to a file
func writeKeyToFile(keyBytes []byte, saveFileTo string) error {
err := ioutil.WriteFile(saveFileTo, keyBytes, 0600)
if err != nil {
return err
}
log.Printf('Key saved to: %s', saveFileTo)
return nil
}
Key

Golang Generate Rsa Key Pem Public Service

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment