Skip to content

Instantly share code, notes, and snippets.

@puiterwijk
Created October 5, 2021 17:29
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 puiterwijk/4d151cf696aeb1cdd86cbfd242eb9786 to your computer and use it in GitHub Desktop.
Save puiterwijk/4d151cf696aeb1cdd86cbfd242eb9786 to your computer and use it in GitHub Desktop.
package main
import (
"os"
"fmt"
"crypto/ecdsa"
"crypto/rand"
"crypto/elliptic"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
)
func main() {
if len(os.Args) != 2 {
panic("Usage: <tool> [hostname]")
}
hostname := os.Args[1]
newkey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
panic(err)
}
subj := pkix.Name {
CommonName: hostname,
}
template := x509.CertificateRequest {
PublicKey: newkey,
Subject: subj,
DNSNames: []string{hostname},
}
req, err := x509.CreateCertificateRequest(rand.Reader, &template, newkey)
if err != nil {
panic(err)
}
privkey, err := x509.MarshalPKCS8PrivateKey(newkey)
if err != nil {
panic(err)
}
privkeyB := pem.EncodeToMemory(&pem.Block {
Type: "PRIVATE KEY",
Bytes: privkey,
})
csrB := pem.EncodeToMemory(&pem.Block {
Type: "CERTIFICATE REQUEST",
Bytes: req,
})
fmt.Println(string(privkeyB))
fmt.Println(string(csrB))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment