refactor: 优化代码

This commit is contained in:
Fu Diwei
2024-07-18 15:56:20 +08:00
parent 08c675c6ba
commit fdb2d2a982
11 changed files with 38 additions and 22 deletions

View File

@@ -17,7 +17,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
CertificateEntry? entry = client.PlatformCertificateManager.GetEntry(strSerialNumber);
if (!entry.HasValue)
{
return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate with serial number \"{strSerialNumber}\"."));
return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate matched the serial number \"{strSerialNumber}\"."));
}
return GenerateSignatureResultByCertificate(
@@ -41,7 +41,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
CertificateEntry? entry = await ((ICertificateManagerAsync)client.PlatformCertificateManager).GetEntryAsync(strSerialNumber, cancellationToken).ConfigureAwait(false);
if (!entry.HasValue)
{
return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate with serial number \"{strSerialNumber}\"."));
return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate matched the serial number \"{strSerialNumber}\"."));
}
return GenerateSignatureResultByCertificate(

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net462; netstandard2.0; net6.0</TargetFrameworks>
<TargetFrameworks>net462; netstandard2.0; net6.0; net8.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes>

View File

@@ -55,6 +55,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings
[System.Text.Json.Serialization.JsonConstructor]
public CertificateEntry(string algorithmType, string serialNumber, string certificate, DateTimeOffset effectiveTime, DateTimeOffset expireTime)
{
certificate = certificate?.Trim()!;
if (string.IsNullOrEmpty(algorithmType))
throw new ArgumentException("The value of `algorithmType` can not be empty.", nameof(algorithmType));
if (string.IsNullOrEmpty(certificate))
@@ -63,8 +65,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings
throw new ArgumentException("The value of `serialNumber` can not be empty.", nameof(serialNumber));
if (!ALGORITHM_TYPE_RSA.Equals(algorithmType) && !ALGORITHM_TYPE_SM2.Equals(algorithmType))
throw new ArgumentException("The value of `algorithmType` an invalid value.", nameof(algorithmType));
if (!certificate.Trim().StartsWith("-----BEGIN CERTIFICATE-----") || !certificate.Trim().EndsWith("-----END CERTIFICATE-----"))
throw new ArgumentException("The value of `certificate` is an invalid certificate file content.", nameof(certificate));
if (!(certificate.StartsWith("-----BEGIN CERTIFICATE-----") && certificate.EndsWith("-----END CERTIFICATE-----")))
throw new ArgumentException("The value of `certificate` is an invalid certificate file content. Maybe you forgot to decrypt?", nameof(certificate));
AlgorithmType = algorithmType;
SerialNumber = serialNumber.ToUpper();
@@ -80,12 +82,14 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings
/// <param name="certificate"></param>
public CertificateEntry(string algorithmType, string certificate)
{
certificate = certificate?.Trim()!;
if (string.IsNullOrEmpty(algorithmType))
throw new ArgumentException("The value of `algorithmType` can not be empty.", nameof(algorithmType));
if (string.IsNullOrEmpty(certificate))
throw new ArgumentException("The value of `certificate` can not be empty.", nameof(certificate));
if (!certificate.Trim().StartsWith("-----BEGIN CERTIFICATE-----") || !certificate.Trim().EndsWith("-----END CERTIFICATE-----"))
throw new ArgumentException("The value of `certificate` is an invalid certificate file content.", nameof(certificate));
if (!(certificate.StartsWith("-----BEGIN CERTIFICATE-----") && certificate.EndsWith("-----END CERTIFICATE-----")))
throw new ArgumentException("The value of `certificate` is an invalid certificate file content. Maybe you forgot to decrypt?", nameof(certificate));
AlgorithmType = algorithmType;
Certificate = certificate;

View File

@@ -45,8 +45,12 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
{
if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase))
throw new NotSupportedException();
#if NET8_0_OR_GREATER
using (AesGcm aes = new AesGcm(keyBytes, TAG_LENGTH_BYTE))
#else
using (AesGcm aes = new AesGcm(keyBytes))
#endif
{
byte[] cipherWithoutTagBytes = new byte[cipherBytes.Length - TAG_LENGTH_BYTE];
byte[] tagBytes = new byte[TAG_LENGTH_BYTE];