Skip to content

Instantly share code, notes, and snippets.

@bunyk
Last active June 27, 2018 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bunyk/660fa241fd61320f47d27034cd6070de to your computer and use it in GitHub Desktop.
Save bunyk/660fa241fd61320f47d27034cd6070de to your computer and use it in GitHub Desktop.
RSA Keypair generation
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"log"
)
func main() {
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
log.Fatal(err)
}
pemdata := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
},
)
fmt.Println(string(pemdata))
pemdata = pem.EncodeToMemory(
&pem.Block{
Type: "PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(&privateKey.PublicKey),
},
)
fmt.Println(string(pemdata))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment