mirror of
				https://github.com/go-acme/lego.git
				synced 2025-10-31 16:37:41 +02:00 
			
		
		
		
	* refactor: linting. - errcheck - govet - golint - goconst - spellcheck - ... * refactor: migrate from gometalinter to golangci-lint.
		
			
				
	
	
		
			59 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"crypto"
 | |
| 	"crypto/ecdsa"
 | |
| 	"crypto/elliptic"
 | |
| 	"crypto/rand"
 | |
| 	"crypto/x509"
 | |
| 	"encoding/pem"
 | |
| 	"errors"
 | |
| 	"io/ioutil"
 | |
| 	"os"
 | |
| )
 | |
| 
 | |
| func generatePrivateKey(file string) (crypto.PrivateKey, error) {
 | |
| 	privateKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	keyBytes, err := x509.MarshalECPrivateKey(privateKey)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	pemKey := pem.Block{Type: "EC PRIVATE KEY", Bytes: keyBytes}
 | |
| 
 | |
| 	certOut, err := os.Create(file)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	defer certOut.Close()
 | |
| 
 | |
| 	err = pem.Encode(certOut, &pemKey)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return privateKey, nil
 | |
| }
 | |
| 
 | |
| func loadPrivateKey(file string) (crypto.PrivateKey, error) {
 | |
| 	keyBytes, err := ioutil.ReadFile(file)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	keyBlock, _ := pem.Decode(keyBytes)
 | |
| 
 | |
| 	switch keyBlock.Type {
 | |
| 	case "RSA PRIVATE KEY":
 | |
| 		return x509.ParsePKCS1PrivateKey(keyBlock.Bytes)
 | |
| 	case "EC PRIVATE KEY":
 | |
| 		return x509.ParseECPrivateKey(keyBlock.Bytes)
 | |
| 	}
 | |
| 
 | |
| 	return nil, errors.New("unknown private key type")
 | |
| }
 |