Skip to content

Instantly share code, notes, and snippets.

@LucaIcaro
Created May 14, 2024 14:48
Show Gist options
  • Select an option

  • Save LucaIcaro/d722a8069195a84ba547650d04e5176c to your computer and use it in GitHub Desktop.

Select an option

Save LucaIcaro/d722a8069195a84ba547650d04e5176c to your computer and use it in GitHub Desktop.
Generate ED25519 openssh-compatible ssh key pair. The output is ready to be used in an authorized_host file and in a private ssh key file.
// GenerateSSHKeys generates a private and public ssh key pair with ed25519 algorithm
// and returns the encoded strings
func GenerateED25519SSHKeys() (string, string, error) {
// If rand is nil, crypto/rand.Reader will be used
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
p, err := ssh.MarshalPrivateKey(crypto.PrivateKey(priv), "")
if err != nil {
panic(err)
}
privateKeyPem := pem.EncodeToMemory(p)
privateKeyString := string(privateKeyPem)
publicKey, err := ssh.NewPublicKey(pub)
if err != nil {
panic(err)
}
publicKeyString := "ssh-ed25519" + " " + base64.StdEncoding.EncodeToString(publicKey.Marshal())
return publicKeyString, privateKeyString, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment