Created
May 14, 2024 14:48
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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