func generateSelfSignedCertKey(host string, alternateIPs []net.IP, alternateDNS []string) ([]byte, []byte, error) {
	priv, err := rsa.GenerateKey(cryptorand.Reader, 2048)
	if err != nil {
		return nil, nil, err
	}

	template := x509.Certificate{
		SerialNumber: big.NewInt(1),
		Subject: pkix.Name{
			CommonName: fmt.Sprintf("%s@%d", host, time.Now().Unix()),
		},
		NotBefore: time.Unix(0, 0),
		NotAfter:  time.Now().Add(time.Hour * 24 * 365 * 100),

		KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
		BasicConstraintsValid: true,
		IsCA:                  true,
	}

	if ip := net.ParseIP(host); ip != nil {
		template.IPAddresses = append(template.IPAddresses, ip)
	} else {
		template.DNSNames = append(template.DNSNames, host)
	}

	template.IPAddresses = append(template.IPAddresses, alternateIPs...)
	template.DNSNames = append(template.DNSNames, alternateDNS...)

	derBytes, err := x509.CreateCertificate(cryptorand.Reader, &template, &template, &priv.PublicKey, priv)
	if err != nil {
		return nil, nil, err
	}

	// Generate cert
	certBuffer := bytes.Buffer{}
	if err := pem.Encode(&certBuffer, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
		return nil, nil, err
	}

	// Generate key
	keyBuffer := bytes.Buffer{}
	if err := pem.Encode(&keyBuffer, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}); err != nil {
		return nil, nil, err
	}

	return certBuffer.Bytes(), keyBuffer.Bytes(), nil
}
// generateSelfSignedCertKey creates a self-signed certificate and key for the given host.
// Host may be an IP or a DNS name
// You may also specify additional subject alt names (either ip or dns names) for the certificate
generateSelfSignedCertKey creates a self-signed certificate and key for the given host.
Host may be an IP or a DNS name
You may also specify additional subject alt names (either ip or dns names) for the certificate
// generateSelfSignedCertKey creates a self - signed certificate and key for the given host . // Host may be an IP or a DNS name // You may also specify additional subject alt names ( either ip or dns names ) for the certificate
func generateSelfSignedCertKey(host string, alternateIPs []net.IP, alternateDNS []string) ([]byte, []byte, error) {
	priv, err := rsa.GenerateKey(cryptorand.Reader, 2048)
	if err != nil {
		return nil, nil, err
	}
	template := x509.Certificate{
		SerialNumber: big.NewInt(1),
		Subject: pkix.Name{
			CommonName: fmt.Sprintf("%s@%d", host, time.Now().Unix()),
		},
		NotBefore: time.Unix(0, 0),
		NotAfter:  time.Now().Add(time.Hour * 24 * 365 * 100),
		KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
		BasicConstraintsValid: true,
		IsCA:                  true,
	}
	if ip := net.ParseIP(host); ip != nil {
		template.IPAddresses = append(template.IPAddresses, ip)
	} else {
		template.DNSNames = append(template.DNSNames, host)
	}
	template.IPAddresses = append(template.IPAddresses, alternateIPs...)
	template.DNSNames = append(template.DNSNames, alternateDNS...)
	derBytes, err := x509.CreateCertificate(cryptorand.Reader, &template, &template, &priv.PublicKey, priv)
	if err != nil {
		return nil, nil, err
	}
	certBuffer := bytes.Buffer{}
	if err := pem.Encode(&certBuffer, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
		return nil, nil, err
	}
	keyBuffer := bytes.Buffer{}
	if err := pem.Encode(&keyBuffer, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}); err != nil {
		return nil, nil, err
	}
	return certBuffer.Bytes(), keyBuffer.Bytes(), nil
}
func generateSelfSignedCertKey ( host string , alternateIPs [ ] net . IP , alternateDNS [ ] string ) ( [ ] byte , [ ] byte , error ) { priv , err := rsa . GenerateKey ( cryptorand . Reader , 2048 ) if err != nil { return nil , nil , err } template := x509 . Certificate { SerialNumber : big . NewInt ( 1 ) , Subject : pkix . Name { CommonName : fmt . Sprintf ( " " , host , time . Now ( ) . Unix ( ) ) , } , NotBefore : time . Unix ( 0 , 0 ) , NotAfter : time . Now ( ) . Add ( time . Hour * 24 * 365 * 100 ) , KeyUsage : x509 . KeyUsageKeyEncipherment | x509 . KeyUsageDigitalSignature | x509 . KeyUsageCertSign , ExtKeyUsage : [ ] x509 . ExtKeyUsage { x509 . ExtKeyUsageServerAuth , x509 . ExtKeyUsageClientAuth } , BasicConstraintsValid : true , IsCA : true , } if ip := net . ParseIP ( host ) ; ip != nil { template . IPAddresses = append ( template . IPAddresses , ip ) } else { template . DNSNames = append ( template . DNSNames , host ) } template . IPAddresses = append ( template . IPAddresses , alternateIPs ... ) template . DNSNames = append ( template . DNSNames , alternateDNS ... ) derBytes , err := x509 . CreateCertificate ( cryptorand . Reader , & template , & template , & priv . PublicKey , priv ) if err != nil { return nil , nil , err } certBuffer := bytes . Buffer { } if err := pem . Encode ( & certBuffer , & pem . Block { Type : " " , Bytes : derBytes } ) ; err != nil { return nil , nil , err } keyBuffer := bytes . Buffer { } if err := pem . Encode ( & keyBuffer , & pem . Block { Type : " " , Bytes : x509 . MarshalPKCS1PrivateKey ( priv ) } ) ; err != nil { return nil , nil , err } return certBuffer . Bytes ( ) , keyBuffer . Bytes ( ) , nil }
generateSelfSignedCertKey creates a self-signed certificate and key for the given host.
generateSelfSignedCertKey creates a self - signed certificate and key for the given host .
// Generate cert // Generate key