func GenerateCertificate() func(*cli.Context) error {
	return func(ctx *cli.Context) error {
		if _, err := os.Stat("key.pem"); !os.IsNotExist(err) {
			if !ctx.Bool("overwrite") {
				log.Fatalf("The file already exists. Won't overwrite unless --overwrite is specified.")
			}
		}

		// Duration the certificate is valid for
		validFor := 2 * 365 * 24 * time.Hour

		// Comma separated list of hostnames the certificate is be valid for
		host := "localhost"

		keyBytes, err := createPrivateKey()
		if err != nil {
			log.Fatalf("Failed to generate private key: %v", err)
		}

		keyUsage := x509.KeyUsageDigitalSignature
		keyUsage |= x509.KeyUsageKeyEncipherment

		notBefore := time.Now()
		notAfter := notBefore.Add(validFor)

		serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
		serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)

		if err != nil {
			log.Fatalf("Failed to generate serial number: %v", err)
		}

		template := x509.Certificate{
			SerialNumber: serialNumber,

			NotBefore: notBefore,
			NotAfter:  notAfter,

			KeyUsage:              keyUsage,
			ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
			BasicConstraintsValid: true,
		}

		hosts := strings.Split(host, ",")
		for _, h := range hosts {
			if ip := net.ParseIP(h); ip != nil {
				template.IPAddresses = append(template.IPAddresses, ip)
			} else {
				template.DNSNames = append(template.DNSNames, h)
			}
		}

		derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &keyBytes.PublicKey, keyBytes)
		if err != nil {
			log.Fatalf("Failed to create certificate: %v", err)
		}

		certOut, err := os.Create("cert.pem")
		if err != nil {
			log.Fatalf("Failed to open cert.pem for writing: %v", err)
		}
		if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
			log.Fatalf("Failed to write data to cert.pem: %v", err)
		}
		if err := certOut.Close(); err != nil {
			log.Fatalf("Error closing cert.pem: %v", err)
		}
		log.Print("wrote cert.pem\n")

		return nil
	}
}
// GenerateCertificate creates a self-signed certificate and stores it in a file
GenerateCertificate creates a self-signed certificate and stores it in a file
// GenerateCertificate creates a self - signed certificate and stores it in a file
func GenerateCertificate() func(*cli.Context) error {
	return func(ctx *cli.Context) error {
		if _, err := os.Stat("key.pem"); !os.IsNotExist(err) {
			if !ctx.Bool("overwrite") {
				log.Fatalf("The file already exists. Won't overwrite unless --overwrite is specified.")
			}
		}
		validFor := 2 * 365 * 24 * time.Hour
		host := "localhost"
		keyBytes, err := createPrivateKey()
		if err != nil {
			log.Fatalf("Failed to generate private key: %v", err)
		}
		keyUsage := x509.KeyUsageDigitalSignature
		keyUsage |= x509.KeyUsageKeyEncipherment
		notBefore := time.Now()
		notAfter := notBefore.Add(validFor)
		serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
		serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
		if err != nil {
			log.Fatalf("Failed to generate serial number: %v", err)
		}
		template := x509.Certificate{
			SerialNumber: serialNumber,
			NotBefore: notBefore,
			NotAfter:  notAfter,
			KeyUsage:              keyUsage,
			ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
			BasicConstraintsValid: true,
		}
		hosts := strings.Split(host, ",")
		for _, h := range hosts {
			if ip := net.ParseIP(h); ip != nil {
				template.IPAddresses = append(template.IPAddresses, ip)
			} else {
				template.DNSNames = append(template.DNSNames, h)
			}
		}
		derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &keyBytes.PublicKey, keyBytes)
		if err != nil {
			log.Fatalf("Failed to create certificate: %v", err)
		}
		certOut, err := os.Create("cert.pem")
		if err != nil {
			log.Fatalf("Failed to open cert.pem for writing: %v", err)
		}
		if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
			log.Fatalf("Failed to write data to cert.pem: %v", err)
		}
		if err := certOut.Close(); err != nil {
			log.Fatalf("Error closing cert.pem: %v", err)
		}
		log.Print("wrote cert.pem\n")
		return nil
	}
}
func GenerateCertificate ( ) func ( * cli . Context ) error { return func ( ctx * cli . Context ) error { if _ , err := os . Stat ( " " ) ; ! os . IsNotExist ( err ) { if ! ctx . Bool ( " " ) { log . Fatalf ( " " ) } } validFor := 2 * 365 * 24 * time . Hour host := " " keyBytes , err := createPrivateKey ( ) if err != nil { log . Fatalf ( " " , err ) } keyUsage := x509 . KeyUsageDigitalSignature keyUsage |= x509 . KeyUsageKeyEncipherment notBefore := time . Now ( ) notAfter := notBefore . Add ( validFor ) serialNumberLimit := new ( big . Int ) . Lsh ( big . NewInt ( 1 ) , 128 ) serialNumber , err := rand . Int ( rand . Reader , serialNumberLimit ) if err != nil { log . Fatalf ( " " , err ) } template := x509 . Certificate { SerialNumber : serialNumber , NotBefore : notBefore , NotAfter : notAfter , KeyUsage : keyUsage , ExtKeyUsage : [ ] x509 . ExtKeyUsage { x509 . ExtKeyUsageServerAuth } , BasicConstraintsValid : true , } hosts := strings . Split ( host , " " ) for _ , h := range hosts { if ip := net . ParseIP ( h ) ; ip != nil { template . IPAddresses = append ( template . IPAddresses , ip ) } else { template . DNSNames = append ( template . DNSNames , h ) } } derBytes , err := x509 . CreateCertificate ( rand . Reader , & template , & template , & keyBytes . PublicKey , keyBytes ) if err != nil { log . Fatalf ( " " , err ) } certOut , err := os . Create ( " " ) if err != nil { log . Fatalf ( " " , err ) } if err := pem . Encode ( certOut , & pem . Block { Type : " " , Bytes : derBytes } ) ; err != nil { log . Fatalf ( " " , err ) } if err := certOut . Close ( ) ; err != nil { log . Fatalf ( " " , err ) } log . Print ( " \n " ) return nil } }
GenerateCertificate creates a self-signed certificate and stores it in a file
GenerateCertificate creates a self - signed certificate and stores it in a file
// Duration the certificate is valid for // Comma separated list of hostnames the certificate is be valid for