Skip to content

Instantly share code, notes, and snippets.

@titanous
Created December 13, 2013 23:47
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 titanous/7953648 to your computer and use it in GitHub Desktop.
Save titanous/7953648 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math"
"math/big"
"os"
"time"
)
func main() {
key, _ := rsa.GenerateKey(rand.Reader, 2048)
cert := &x509.Certificate{
Subject: pkix.Name{CommonName: "notary"},
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour * 24 * 365 * 10),
IsCA: true,
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
cert.SerialNumber, _ = rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
certBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, &key.PublicKey, key)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
pem.Encode(os.Stdout, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment