crt

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: MIT Imports: 7 Imported by: 0

README

Certificates Generator

test codecov Go Report Card release license

Getting Started

package main

import (
	"crypto/x509"
	"log"
	"net"
	"os"
	"time"

	"github.com/shipengqi/crt"
	"github.com/shipengqi/crt/generator"
	"github.com/shipengqi/crt/key"
)

func main() {

        // ---------------------------------
	// Create Certificate Examples
	
	// create a certificate
	exCert := crt.New(
		crt.WithCN("example.com"),
		crt.WithKeyUsage(x509.KeyUsageDigitalSignature|x509.KeyUsageKeyEncipherment),
		crt.WithExtKeyUsages(x509.ExtKeyUsageServerAuth),
		crt.WithDNSNames("foo.example.com", "bar.example.com"),
		crt.WithIPs(net.ParseIP("16.187.0.1"), net.ParseIP("16.187.0.2")),
		crt.WithOrganizations("org1"),
		crt.WithValidity(time.Hour*24*365),
	)

	// create a server certificate
	serverCrt := crt.NewServerCert()

	// create a client certificate
	clientCrt := crt.NewClientCert()

	// create a CA certificate
	caCrt := crt.NewCACert()

	// ---------------------------------
	// Create Generator Examples
	
	// create a Generator instance
	// by default, use RSA key generator
	g1 := generator.New()
	// create a Generator instance with specified key generator
	kgen := key.NewEcdsaKey(nil)
	g2 := generator.New(generator.WithKeyGenerator(kgen))

	// ---------------------------------
	// generate Certificate Examples
	
	// generate CA certificate
	cf, _ := os.Create("ca.crt")
	pf, _ := os.Create("ca.key")
	w := generator.NewFileWriter(cf, pf)
	err := g1.CreateAndWrite(w, caCrt)
	if err != nil {
		log.Fatalln(err)
	}
	
	// generate server certificate
	// set the CA for the generator
	_, _, err = g1.CreateWithOptions(caCrt, generator.CreateOptions{
		UseAsCA: true,
	})
	// generate server certificate files
	w, _ = generator.NewFileWriterFromPaths("server.crt", "server.key")
	err = g1.CreateAndWrite(w, serverCrt)
	if err != nil {
		log.Fatalln(err)
	}
}

Documentation

You can find the docs at go docs.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Certificate

type Certificate struct {
	// contains filtered or unexported fields
}

Certificate is the main structure of a Certificate.

func New

func New(opts ...Option) *Certificate

New create a new Certificate.

func NewCACert

func NewCACert(opts ...Option) *Certificate

NewCACert create a new CA Certificate.

func NewClientCert

func NewClientCert(opts ...Option) *Certificate

NewClientCert create a new Client Certificate.

func NewServerCert

func NewServerCert(opts ...Option) *Certificate

NewServerCert create a new Server Certificate.

func (*Certificate) Gen

func (c *Certificate) Gen() *x509.Certificate

Gen generates a new x509.Certificate.

func (*Certificate) IsCA

func (c *Certificate) IsCA() bool

IsCA return whether the certificate is a CA certificate.

func (*Certificate) IsClientCert

func (c *Certificate) IsClientCert() bool

IsClientCert return whether the certificate is a Client certificate.

func (*Certificate) IsServerCert

func (c *Certificate) IsServerCert() bool

IsServerCert return whether the certificate is a Server certificate.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option defines optional parameters for initializing the certificate structure.

func WithCAType

func WithCAType() Option

WithCAType is used to set the CA certificate type.

func WithCN

func WithCN(cn string) Option

WithCN is used to set the CommonName.

func WithClientType

func WithClientType() Option

WithClientType is used to set the Client certificate type.

func WithDNSNames

func WithDNSNames(dns ...string) Option

WithDNSNames is used to set the DNS Name values of the certificate.

func WithExtKeyUsages

func WithExtKeyUsages(extKeyUsage ...x509.ExtKeyUsage) Option

WithExtKeyUsages is used to set the x509.ExtKeyUsage values of the certificate.

func WithIPs

func WithIPs(ip ...net.IP) Option

WithIPs is used to set the IP Address values of the certificate.

func WithKeyUsage

func WithKeyUsage(keyUsage ...x509.KeyUsage) Option

WithKeyUsage is used to set the x509.KeyUsage of the certificate.

func WithOrganizations added in v0.1.6

func WithOrganizations(org ...string) Option

WithOrganizations is used to set the Organization values of the certificate.

func WithServerType

func WithServerType() Option

WithServerType is used to set the Server certificate type.

func WithValidity

func WithValidity(validity time.Duration) Option

WithValidity is used to set the validity of the certificate.

Directories

Path Synopsis
Package generator defines a generator used to create certificate and private key.
Package generator defines a generator used to create certificate and private key.

Jump to

Keyboard shortcuts

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