github.com/decred/politeia@v1.4.0/util/cert.go (about)

     1  // Copyright (c) 2017 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  import (
     8  	"crypto/elliptic"
     9  	"os"
    10  	"time"
    11  
    12  	"github.com/decred/dcrd/certgen"
    13  )
    14  
    15  // GenCertPair generates a key/cert pair to the paths provided.
    16  func GenCertPair(curve elliptic.Curve, org, certFile, keyFile string) error {
    17  	validUntil := time.Now().Add(10 * 365 * 24 * time.Hour)
    18  	cert, key, err := certgen.NewTLSCertPair(curve, org, validUntil, nil)
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	// Write cert and key files.
    24  	if err = os.WriteFile(certFile, cert, 0666); err != nil {
    25  		return err
    26  	}
    27  	if err = os.WriteFile(keyFile, key, 0600); err != nil {
    28  		os.Remove(certFile)
    29  		return err
    30  	}
    31  
    32  	return nil
    33  }