123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Org.BouncyCastle.X509;
- using Alipay.EasySDK.Kernel.Util;
- using System.Linq;
- namespace Alipay.EasySDK.Kernel
- {
-
-
-
- public class CertEnvironment
- {
-
-
-
- public string RootCertContent { get; set; }
-
-
-
- public string RootCertSN { get; set; }
-
-
-
- public string MerchantCertSN { get; set; }
-
-
-
- private readonly Dictionary<string, string> CachedAlipayPublicKey = new Dictionary<string, string>();
-
-
-
-
-
-
- public CertEnvironment(string merchantCertPath, string alipayCertPath, string alipayRootCertPath)
- {
- if (string.IsNullOrEmpty(merchantCertPath) || string.IsNullOrEmpty(alipayCertPath) || string.IsNullOrEmpty(alipayCertPath))
- {
- throw new Exception("证书参数merchantCertPath、alipayCertPath或alipayRootCertPath设置不完整。");
- }
- this.RootCertContent = File.ReadAllText(alipayRootCertPath);
- this.RootCertSN = AntCertificationUtil.GetRootCertSN(RootCertContent);
- X509Certificate merchantCert = AntCertificationUtil.ParseCert(File.ReadAllText(merchantCertPath));
- this.MerchantCertSN = AntCertificationUtil.GetCertSN(merchantCert);
- X509Certificate alipayCert = AntCertificationUtil.ParseCert(File.ReadAllText(alipayCertPath));
- string alipayCertSN = AntCertificationUtil.GetCertSN(alipayCert);
- string alipayPublicKey = AntCertificationUtil.ExtractPemPublicKeyFromCert(alipayCert);
- CachedAlipayPublicKey[alipayCertSN] = alipayPublicKey;
- }
- public string GetAlipayPublicKey(string sn)
- {
-
- if (string.IsNullOrEmpty(sn))
- {
- return CachedAlipayPublicKey.Values.FirstOrDefault();
- }
- if (CachedAlipayPublicKey.ContainsKey(sn))
- {
- return CachedAlipayPublicKey[sn];
- }
- else
- {
-
-
- throw new Exception("支付宝公钥证书[" + sn + "]已过期,请重新下载最新支付宝公钥证书并替换原证书文件");
- }
- }
- }
- }
|