import ( "crypto/rsa" "encoding/pem" "golang.org/x/crypto/ssh" ) func main() { // generate a dummy RSA public key rsaKey, _ := rsa.GenerateKey(rand.Reader, 2048) publicKey := &rsaKey.PublicKey // convert the public key to bytes using marshal sshKey, _ := ssh.NewPublicKey(publicKey) publicKeyBytes := sshKey.Marshal() // encode the public key into PEM format for storage pemKey := pem.EncodeToMemory(&pem.Block{ Type: "RSA PUBLIC KEY", Bytes: publicKeyBytes, }) }
import ( "crypto/ed25519" "encoding/hex" "golang.org/x/crypto/ssh" ) func main() { // generate a dummy Ed25519 public key ed25519Key, _ := ed25519.GenerateKey(nil) publicKey := ed25519Key.Public().(ed25519.PublicKey) // convert the public key to bytes using marshal sshKey, _ := ssh.NewPublicKey(publicKey) publicKeyBytes := sshKey.Marshal() // display the public key bytes in hex publicKeyHex := hex.EncodeToString(publicKeyBytes) fmt.Println(publicKeyHex) }This example generates a dummy Ed25519 public key, converts it to a byte array using PublicKey Marshal, and displays the public key bytes in hex. Both examples use the golang.org.x.crypto.ssh package library.