github.com/v2fly/v2ray-core/v4@v4.45.2/infra/control/certchainhash.go (about)

     1  package control
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  
     8  	v2tls "github.com/v2fly/v2ray-core/v4/transport/internet/tls"
     9  )
    10  
    11  type CertificateChainHashCommand struct{}
    12  
    13  func (c CertificateChainHashCommand) Name() string {
    14  	return "certChainHash"
    15  }
    16  
    17  func (c CertificateChainHashCommand) Description() Description {
    18  	return Description{
    19  		Short: "Calculate TLS certificates hash.",
    20  		Usage: []string{
    21  			"v2ctl certChainHash --cert <cert.pem>",
    22  			"Calculate TLS certificate chain hash.",
    23  		},
    24  	}
    25  }
    26  
    27  func (c CertificateChainHashCommand) Execute(args []string) error {
    28  	fs := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
    29  	cert := fs.String("cert", "fullchain.pem", "The file path of the certificates chain")
    30  	if err := fs.Parse(args); err != nil {
    31  		return err
    32  	}
    33  	certContent, err := os.ReadFile(*cert)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	certChainHashB64 := v2tls.CalculatePEMCertChainSHA256Hash(certContent)
    38  	fmt.Println(certChainHashB64)
    39  	return nil
    40  }
    41  
    42  func init() {
    43  	// Do not release tool before v5's refactor
    44  	// common.Must(RegisterCommand(&CertificateChainHashCommand{}))
    45  }