PHP openssl_pkcs12_read to Go: Convert PKCS#12 Certificate to PEM
{/'title/':/'PHP openssl_pkcs12_read to Go: Convert PKCS#12 Certificate to PEM/',/'description/':/'This article explains how to convert PHP's openssl_pkcs12_read function to its equivalent Go code using the pkcs12 and crypto/x509 packages. Learn how to load and extract a private key from a PKCS#12 certificate in Go./',/'keywords/':/'Go, PHP, openssl_pkcs12_read, PKCS#12, certificate, PEM, private key, crypto/x509, pkcs12/',/'content/':/'///'$file = file_get_contents($path); if (!$file) { //tthrow new //Exception('loadPk12Cert::file_get_contents'); } if (!openssl_pkcs12_read($file, $cert, $pwd)) { //tthrow new //Exception('loadPk12Cert::openssl_pkcs12_read ERROR'); } return $cert['pkey'];///' 帮我转换成go语言内容:以下是将给定的PHP代码转换为Go语言的等效代码://n//ngo//npackage main//n//nimport (//n//t/'crypto/x509/'//n//t/'encoding/pem/'//n//t/'fmt/'//n//t/'io/ioutil/'//n//t/'log/'//n)//n//nfunc loadPk12Cert(path string, pwd []byte) ([]byte, error) {//n//tfile, err := ioutil.ReadFile(path)//n//tif err != nil {//n//t//treturn nil, fmt.Errorf(/'loadPk12Cert: ioutil.ReadFile - %w/', err)//n//t}//n//n//tblocks, err := pkcs12ToPem(file, pwd)//n//tif err != nil {//n//t//treturn nil, fmt.Errorf(/'loadPk12Cert: pkcs12ToPem - %w/', err)//n//t}//n//n//treturn blocks[0].Bytes, nil//n}//n//nfunc pkcs12ToPem(file []byte, pwd []byte) ([]*pem.Block, error) {//n//tblocks, err := pkcs12.ToPEM(file, string(pwd))//n//tif err != nil {//n//t//treturn nil, fmt.Errorf(/'pkcs12ToPem: pkcs12.ToPEM - %w/', err)//n//t}//n//n//treturn blocks, nil//n}//n//nfunc main() {//n//tpath := /'path/to/certificate.p12/'//n//tpwd := []byte(/'password/')//n//n//tcert, err := loadPk12Cert(path, pwd)//n//tif err != nil {//n//t//tlog.Fatal(err)//n//t}//n//n//tfmt.Println(cert)//n}//n//n//n请注意,这个等效的Go代码使用了pkcs12包来实现与PHP中的openssl_pkcs12_read函数相同的功能。在运行此代码之前,请确保已经在Go代码中引入了pkcs12和crypto/x509包。/
原文地址: https://www.cveoy.top/t/topic/pNbZ 著作权归作者所有。请勿转载和采集!