feat(tenpayv3): 优化反射性能

This commit is contained in:
Fu Diwei 2021-12-03 16:53:00 +08:00
parent ec6e3f7e7a
commit 96f1f262e7
4 changed files with 44 additions and 29 deletions

View File

@ -27,11 +27,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
if (requireEncrypt) if (requireEncrypt)
{ {
// 遍历并加密被标记为敏感数据的字段 // 遍历并加密被标记为敏感数据的字段
Utilities.ReflectionUtility.ReplacePropertyStringValue(ref request, (obj, prop, value) => Utilities.ReflectionUtility.ReplacePropertyStringValue(ref request, (target, currentProp, oldValue) =>
{ {
var attr = prop.GetCustomAttribute<WechatTenpaySensitivePropertyAttribute>(); var attr = currentProp.GetCustomAttribute<WechatTenpaySensitivePropertyAttribute>();
if (attr == null) if (attr == null)
return value; return (false, oldValue);
if (Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB.Equals(attr.Algorithm)) if (Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB.Equals(attr.Algorithm))
{ {
@ -65,10 +65,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
request.WechatpayCertSerialNumber = cert.SerialNumber; request.WechatpayCertSerialNumber = cert.SerialNumber;
} }
return Utilities.RSAUtility.EncryptWithECBByCertificate( string newValue = Utilities.RSAUtility.EncryptWithECBByCertificate(
certificate: certificate, certificate: certificate,
plainText: value plainText: oldValue
); );
return (true, newValue);
} }
else else
{ {

View File

@ -59,18 +59,19 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
if (requireDecrypt) if (requireDecrypt)
{ {
// 遍历并解密被标记为敏感数据的字段 // 遍历并解密被标记为敏感数据的字段
Utilities.ReflectionUtility.ReplacePropertyStringValue(ref response, (obj, prop, value) => Utilities.ReflectionUtility.ReplacePropertyStringValue(ref response, (target, currentProp, oldValue) =>
{ {
var attr = prop.GetCustomAttribute<WechatTenpaySensitivePropertyAttribute>(); var attr = currentProp.GetCustomAttribute<WechatTenpaySensitivePropertyAttribute>();
if (attr == null) if (attr == null)
return value; return (false, oldValue);
if (Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB.Equals(attr.Algorithm)) if (Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB.Equals(attr.Algorithm))
{ {
return Utilities.RSAUtility.DecryptWithECB( string newValue = Utilities.RSAUtility.DecryptWithECB(
privateKey: client.Credentials.MerchantCertPrivateKey, privateKey: client.Credentials.MerchantCertPrivateKey,
cipherText: value cipherText: oldValue
); );
return (true, newValue);
} }
else else
{ {

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net461; netstandard2.0; net5.0; net6.0</TargetFrameworks> <TargetFrameworks>net461; net47; netstandard2.0; net5.0; net6.0</TargetFrameworks>
<LangVersion>8.0</LangVersion> <LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes> <NullableReferenceTypes>true</NullableReferenceTypes>
@ -23,10 +23,11 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System.Web" Condition="'$(TargetFramework)' == 'net461'" /> <Reference Include="System.Web" Condition="'$(TargetFramework)' == 'net461' Or '$(TargetFramework)' == 'net47'" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.ValueTuple" Version="4.5.0" Condition="'$(TargetFramework)' == 'net461'" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" /> <PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.0.0" /> <PackageReference Include="SKIT.FlurlHttpClient.Common" Version="2.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -6,14 +6,14 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
{ {
internal static class ReflectionUtility internal static class ReflectionUtility
{ {
public delegate string ReplacePropertyStringValueReplacement(object obj, PropertyInfo prop, string value); public delegate (bool Modified, string NewValue) ReplacePropertyStringValueReplacement(object target, PropertyInfo currentProp, string oldValue);
public static void ReplacePropertyStringValue<T>(ref T obj, ReplacePropertyStringValueReplacement replacement) public static void ReplacePropertyStringValue<T>(ref T obj, ReplacePropertyStringValueReplacement replacement)
{ {
InnerReplacePropertyStringValue(ref obj, replacement, null); InnerReplacePropertyStringValue(ref obj, replacement);
} }
private static void InnerReplacePropertyStringValue<T>(ref T obj, ReplacePropertyStringValueReplacement replacement, PropertyInfo? currentProp) private static void InnerReplacePropertyStringValue<T>(ref T obj, ReplacePropertyStringValueReplacement replacement)
{ {
if (obj == null) throw new ArgumentNullException(nameof(obj)); if (obj == null) throw new ArgumentNullException(nameof(obj));
if (replacement == null) throw new ArgumentNullException(nameof(replacement)); if (replacement == null) throw new ArgumentNullException(nameof(replacement));
@ -37,8 +37,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
if (propType == typeof(string)) if (propType == typeof(string))
{ {
string oldValue = (string)childProp.GetValue(obj, null)!; string oldValue = (string)childProp.GetValue(obj, null)!;
string newValue = replacement(obj, childProp, oldValue); var result = replacement(obj, childProp, oldValue);
childProp.SetValue(obj, newValue); if (result.Modified)
{
childProp.SetValue(obj, result.NewValue);
}
} }
else if (propType.IsClass) else if (propType.IsClass)
{ {
@ -46,7 +49,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
if (value is null) if (value is null)
continue; continue;
InnerReplacePropertyStringValue(ref value, replacement, childProp); InnerReplacePropertyStringValue(ref value, replacement);
childProp.SetValue(obj, value); childProp.SetValue(obj, value);
} }
else else
@ -83,13 +86,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
if (!currentProp.CanWrite) if (!currentProp.CanWrite)
continue; continue;
string oldValue = (string)element!; var oldValue = (string)element!;
string newValue = replacement(obj!, currentProp, oldValue); var resHandler = replacement(obj!, currentProp, oldValue);
array.SetValue(newValue, i); if (resHandler.Modified)
{
array.SetValue(resHandler.NewValue, i);
}
} }
else if (elementType.IsClass) else if (elementType.IsClass)
{ {
InnerReplacePropertyStringValue(ref element, replacement, currentProp); InnerReplacePropertyStringValue(ref element, replacement);
array.SetValue(element, i); array.SetValue(element, i);
} }
else else
@ -118,13 +124,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
if (!currentProp.CanWrite) if (!currentProp.CanWrite)
continue; continue;
string oldValue = (string)element!; var oldValue = (string)element!;
string newValue = replacement(obj, currentProp, oldValue); var resHandler = replacement(obj, currentProp, oldValue);
list[i] = newValue; if (resHandler.Modified)
{
list[i] = resHandler.NewValue;
}
} }
else if (elementType.IsClass) else if (elementType.IsClass)
{ {
InnerReplacePropertyStringValue(ref element, replacement, currentProp); InnerReplacePropertyStringValue(ref element, replacement);
list[i] = element; list[i] = element;
} }
else else
@ -154,12 +163,15 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
continue; continue;
string oldValue = (string)entryValue!; string oldValue = (string)entryValue!;
string newValue = replacement(obj, currentProp, oldValue); var resHandler = replacement(obj, currentProp, oldValue);
dict[entry.Key] = newValue; if (resHandler.Modified)
{
dict[entry.Key] = resHandler.NewValue;
}
} }
else if (entryValueType.IsClass) else if (entryValueType.IsClass)
{ {
InnerReplacePropertyStringValue(ref entryValue, replacement, currentProp); InnerReplacePropertyStringValue(ref entryValue, replacement);
dict[entry.Key] = entryValue; dict[entry.Key] = entryValue;
} }
else else