| // +build ignore |
| |
| package main |
| |
| import ( |
| "crypto/rand" |
| "crypto/rsa" |
| "crypto/x509" |
| "encoding/pem" |
| "os" |
| ) |
| |
| func main() { |
| |
| f1, err := os.Create("sample_key.priv") |
| if err != nil { |
| panic(err) |
| } |
| f2, err := os.Create("sample_key.pub") |
| if err != nil { |
| panic(err) |
| } |
| |
| privateKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| if err != nil { |
| panic(err) |
| } |
| |
| privateKeyDer := x509.MarshalPKCS1PrivateKey(privateKey) |
| privateKeyBlock := pem.Block{ |
| Type: "RSA PRIVATE KEY", |
| Headers: nil, |
| Bytes: privateKeyDer, |
| } |
| pem.Encode(f1, &privateKeyBlock) |
| |
| publicKey := privateKey.PublicKey |
| publicKeyDer, err := x509.MarshalPKIXPublicKey(&publicKey) |
| if err != nil { |
| panic(err) |
| } |
| |
| publicKeyBlock := pem.Block{ |
| Type: "PUBLIC KEY", |
| Headers: nil, |
| Bytes: publicKeyDer, |
| } |
| pem.Encode(f2, &publicKeyBlock) |
| |
| f1.Close() |
| f2.Close() |
| } |