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

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

View File

@ -93,7 +93,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase)) if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase))
throw new NotSupportedException(); throw new NotSupportedException();
#if NET8_0_OR_GREATER
using (AesGcm aes = new AesGcm(keyBytes, TAG_LENGTH_BYTE))
#else
using (AesGcm aes = new AesGcm(keyBytes)) using (AesGcm aes = new AesGcm(keyBytes))
#endif
{ {
byte[] cipherWithoutTagBytes = new byte[cipherBytes.Length - TAG_LENGTH_BYTE]; byte[] cipherWithoutTagBytes = new byte[cipherBytes.Length - TAG_LENGTH_BYTE];
byte[] tagBytes = new byte[TAG_LENGTH_BYTE]; byte[] tagBytes = new byte[TAG_LENGTH_BYTE];
@ -216,7 +220,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase)) if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase))
throw new NotSupportedException(); throw new NotSupportedException();
#if NET8_0_OR_GREATER
using (AesGcm aes = new AesGcm(keyBytes, TAG_LENGTH_BYTE))
#else
using (AesGcm aes = new AesGcm(keyBytes)) using (AesGcm aes = new AesGcm(keyBytes))
#endif
{ {
byte[] cipherBytes = new byte[plainBytes.Length]; byte[] cipherBytes = new byte[plainBytes.Length];
byte[] tagBytes = new byte[TAG_LENGTH_BYTE]; byte[] tagBytes = new byte[TAG_LENGTH_BYTE];

View File

@ -17,7 +17,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
CertificateEntry? entry = client.PlatformCertificateManager.GetEntry(strSerialNumber); CertificateEntry? entry = client.PlatformCertificateManager.GetEntry(strSerialNumber);
if (!entry.HasValue) 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( return GenerateSignatureResultByCertificate(
@ -41,7 +41,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
CertificateEntry? entry = await ((ICertificateManagerAsync)client.PlatformCertificateManager).GetEntryAsync(strSerialNumber, cancellationToken).ConfigureAwait(false); CertificateEntry? entry = await ((ICertificateManagerAsync)client.PlatformCertificateManager).GetEntryAsync(strSerialNumber, cancellationToken).ConfigureAwait(false);
if (!entry.HasValue) 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( return GenerateSignatureResultByCertificate(

View File

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

View File

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

View File

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

View File

@ -6,18 +6,18 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance.InteropServices
{ {
internal static class MarshalerHelper internal static class MarshalerHelper
{ {
public static string PtrToStringAnsi(IntPtr ptr) public static string? PtrToStringAnsi(IntPtr ptr)
{ {
if (ptr == IntPtr.Zero) if (ptr == IntPtr.Zero)
return default!; return null;
return Marshal.PtrToStringAnsi(ptr)!; return Marshal.PtrToStringAnsi(ptr)!;
} }
public static string PtrToStringUTF8(IntPtr ptr) public static string? PtrToStringUTF8(IntPtr ptr)
{ {
if (ptr == IntPtr.Zero) if (ptr == IntPtr.Zero)
return default!; return null;
#if NETCOREAPP || NET5_0_OR_GREATER #if NETCOREAPP || NET5_0_OR_GREATER
return Marshal.PtrToStringUTF8(ptr)!; return Marshal.PtrToStringUTF8(ptr)!;

View File

@ -159,7 +159,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance
IsRunOnWindows() ? FinanceDllWindowsPInvoker.GetContentFromSlice(dataPtr) : IsRunOnWindows() ? FinanceDllWindowsPInvoker.GetContentFromSlice(dataPtr) :
IsRunOnLinux() ? FinanceDllLinuxPInvoker.GetContentFromSlice(dataPtr) : IsRunOnLinux() ? FinanceDllLinuxPInvoker.GetContentFromSlice(dataPtr) :
throw new PlatformNotSupportedException(); throw new PlatformNotSupportedException();
string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr); string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr)!;
response = JsonSerializer.Deserialize<Models.GetChatRecordsResponse>(dataContent); response = JsonSerializer.Deserialize<Models.GetChatRecordsResponse>(dataContent);
response._InternalRawBytes = Encoding.UTF8.GetBytes(dataContent); response._InternalRawBytes = Encoding.UTF8.GetBytes(dataContent);
@ -253,7 +253,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance
IsRunOnWindows() ? FinanceDllWindowsPInvoker.GetContentFromSlice(dataPtr) : IsRunOnWindows() ? FinanceDllWindowsPInvoker.GetContentFromSlice(dataPtr) :
IsRunOnLinux() ? FinanceDllLinuxPInvoker.GetContentFromSlice(dataPtr) : IsRunOnLinux() ? FinanceDllLinuxPInvoker.GetContentFromSlice(dataPtr) :
throw new PlatformNotSupportedException(); throw new PlatformNotSupportedException();
string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr); string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr)!;
try try
{ {

View File

@ -7,11 +7,6 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance
/// </summary> /// </summary>
public abstract class WechatWorkFinanceResponse : ICommonResponse public abstract class WechatWorkFinanceResponse : ICommonResponse
{ {
internal protected WechatWorkFinanceResponse()
{
_InternalRawBytes = Array.Empty<byte>();
}
[Newtonsoft.Json.JsonIgnore] [Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore] [System.Text.Json.Serialization.JsonIgnore]
internal byte[] _InternalRawBytes; internal byte[] _InternalRawBytes;
@ -34,6 +29,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance
return _InternalRawBytes; return _InternalRawBytes;
} }
internal protected WechatWorkFinanceResponse()
{
_InternalRawBytes = Array.Empty<byte>();
}
/// <summary> /// <summary>
/// 获取企业微信会话内容存档 API 返回的返回值。 /// 获取企业微信会话内容存档 API 返回的返回值。
/// </summary> /// </summary>

View File

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

View File

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