cert.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package unionpay
  2. import (
  3. "crypto/rsa"
  4. "crypto/x509"
  5. "encoding/pem"
  6. "errors"
  7. "fmt"
  8. "io/ioutil"
  9. "golang.org/x/crypto/pkcs12"
  10. )
  11. // getPublicCert 获取加密证书信息
  12. func getPublicCert(filename string) (cert *x509.Certificate, data []byte, err error) {
  13. data, err = ioutil.ReadFile(filename)
  14. if err != nil {
  15. return
  16. }
  17. block, _ := pem.Decode(data)
  18. if block == nil {
  19. err = errors.New(`pem Decode error`)
  20. return
  21. }
  22. cert, err = x509.ParseCertificate(block.Bytes)
  23. return
  24. }
  25. // getPrivateCert 获取签名证书信息
  26. func getPrivateCert(filename, password string) (key *rsa.PrivateKey, cert *x509.Certificate, err error) {
  27. var (
  28. bs []byte
  29. private interface{}
  30. )
  31. bs, err = ioutil.ReadFile(filename)
  32. if err != nil {
  33. fmt.Println(err)
  34. return
  35. }
  36. /*
  37. block, _ := pem.Decode(bs)
  38. if block == nil {
  39. err = errors.New(`PrivateKey pem Decode error`)
  40. return
  41. }
  42. cert = &x509.Certificate{}
  43. private, err = x509.ParsePKCS8PrivateKey(block.Bytes)
  44. // */
  45. //*
  46. private, cert, err = pkcs12.Decode(bs, password)
  47. if err != nil {
  48. return
  49. }
  50. //fmt.Println(cert.SerialNumber)
  51. // */
  52. key = private.(*rsa.PrivateKey)
  53. return
  54. }