mirror of
https://github.com/apricote/hcloud-upload-image.git
synced 2026-01-13 21:31:03 +00:00
45 lines
816 B
Go
45 lines
816 B
Go
package sshkey
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"encoding/pem"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
func GenerateKeyPair() ([]byte, []byte, error) {
|
|
pub, priv, err := ed25519.GenerateKey(nil)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
pubBytes, err := encodePublicKey(pub)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
privBytes, err := encodePrivateKey(priv)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return pubBytes, privBytes, nil
|
|
}
|
|
|
|
func encodePublicKey(pub ed25519.PublicKey) ([]byte, error) {
|
|
sshPub, err := ssh.NewPublicKey(pub)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ssh.MarshalAuthorizedKey(sshPub), nil
|
|
}
|
|
|
|
func encodePrivateKey(priv ed25519.PrivateKey) ([]byte, error) {
|
|
privPem, err := ssh.MarshalPrivateKey(priv, "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return pem.EncodeToMemory(privPem), nil
|
|
}
|