12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package unionpay
- import (
- "crypto/rsa"
- "crypto/x509"
- "encoding/pem"
- "errors"
- "fmt"
- "io/ioutil"
- "golang.org/x/crypto/pkcs12"
- )
- // getPublicCert 获取加密证书信息
- func getPublicCert(filename string) (cert *x509.Certificate, data []byte, err error) {
- data, err = ioutil.ReadFile(filename)
- if err != nil {
- return
- }
- block, _ := pem.Decode(data)
- if block == nil {
- err = errors.New(`pem Decode error`)
- return
- }
- cert, err = x509.ParseCertificate(block.Bytes)
- return
- }
- // getPrivateCert 获取签名证书信息
- func getPrivateCert(filename, password string) (key *rsa.PrivateKey, cert *x509.Certificate, err error) {
- var (
- bs []byte
- private interface{}
- )
- bs, err = ioutil.ReadFile(filename)
- if err != nil {
- fmt.Println(err)
- return
- }
- /*
- block, _ := pem.Decode(bs)
- if block == nil {
- err = errors.New(`PrivateKey pem Decode error`)
- return
- }
- cert = &x509.Certificate{}
- private, err = x509.ParsePKCS8PrivateKey(block.Bytes)
- // */
- //*
- private, cert, err = pkcs12.Decode(bs, password)
- if err != nil {
- return
- }
- //fmt.Println(cert.SerialNumber)
- // */
- key = private.(*rsa.PrivateKey)
- return
- }
|