gencert

package module
v0.0.0-...-6f434bb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2023 License: MIT Imports: 9 Imported by: 0

README

Go Doc Go Report Card

gencert

gencert provides a simple mechanism to create self-signed x509 certificates.

Use

For default settings, call NewDefaultCertificate, which will return a map containing the location of the temporary files storing the new certificate and key using PEM encoding.

For more granular behaviour, use SelfSignedCertGenerator.

func main() {
	// Basic information needed for the certificate creation
	commonName := flag.String("n", "localhost", "Common name for certificate")
	timeout := flag.Int("t", 10, "Hours to certificate expiry")
	flag.Parse()

	// Create a self-signed certificate for TLS
	ssl, err := NewDefaultCertificate(*commonName, time.Duration(*timeout)*time.Hour)
	if err != nil {
		log.Fatal(err)
	}
	// As the certificate is self-signed, it needs to be added to clients
	// This log allows the certificate location to be found
	log.Printf("Certificate saved to: %s\n", ssl["cert"])

	// Set up server
	mux := http.NewServeMux()
	// add handers ...

	// Start server
	log.Fatal(http.ListenAndServeTLS(":443", ssl["cert"], ssl["key"], mux))
}

How?

This command line is all you need.

go get github.com/gford1000-go/gencert

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDefaultCertificate

func NewDefaultCertificate(commonName string, ttl time.Duration) (map[string]string, error)

NewDefaultCertificate creates a self-signed certificate and key and saves them to temporary files whose names are returned, using default values.

Key for certificate: "cert"
Key for private key: "key"

Types

type Encoder

type Encoder func([]byte) []byte

type SelfSignedCert

type SelfSignedCert struct {
	Cert    []byte
	Expires time.Time
	Key     []byte
}

SelfSignedCert is a key/cert pair created by an instance of SelfSignedCertGenerator

func (*SelfSignedCert) SaveTempFiles

func (s *SelfSignedCert) SaveTempFiles() (string, string, error)

SaveTempFiles writes the key and certificate to temporary files Left to caller to clear away the files after use

func (*SelfSignedCert) String

func (s *SelfSignedCert) String() string

String returns the cert only

type SelfSignedCertGenerator

type SelfSignedCertGenerator struct {
	Bits        int     // The length of the private key - defaults to 4096
	CertEncoder Encoder // The encoder for Certificate - defaults to PEM
	KeyEncoder  Encoder // The encoder for Key - defaults to PEM
}

SelfSignedCertGenerator provides flexibility in certificate generation

func (*SelfSignedCertGenerator) Create

func (s *SelfSignedCertGenerator) Create(commonName string, ttl time.Duration) (*SelfSignedCert, error)

Create generates a SelfSignedCert for the common name and lifetime

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL