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)
{
// 遍历并加密被标记为敏感数据的字段
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)
return value;
return (false, oldValue);
if (Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB.Equals(attr.Algorithm))
{
@ -65,10 +65,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
request.WechatpayCertSerialNumber = cert.SerialNumber;
}
return Utilities.RSAUtility.EncryptWithECBByCertificate(
string newValue = Utilities.RSAUtility.EncryptWithECBByCertificate(
certificate: certificate,
plainText: value
plainText: oldValue
);
return (true, newValue);
}
else
{

View File

@ -59,18 +59,19 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
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)
return value;
return (false, oldValue);
if (Constants.EncryptionAlgorithms.RSA_2048_PKCS8_ECB.Equals(attr.Algorithm))
{
return Utilities.RSAUtility.DecryptWithECB(
string newValue = Utilities.RSAUtility.DecryptWithECB(
privateKey: client.Credentials.MerchantCertPrivateKey,
cipherText: value
cipherText: oldValue
);
return (true, newValue);
}
else
{

View File

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

View File

@ -6,14 +6,14 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
{
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)
{
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 (replacement == null) throw new ArgumentNullException(nameof(replacement));
@ -37,8 +37,11 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
if (propType == typeof(string))
{
string oldValue = (string)childProp.GetValue(obj, null)!;
string newValue = replacement(obj, childProp, oldValue);
childProp.SetValue(obj, newValue);
var result = replacement(obj, childProp, oldValue);
if (result.Modified)
{
childProp.SetValue(obj, result.NewValue);
}
}
else if (propType.IsClass)
{
@ -46,7 +49,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
if (value is null)
continue;
InnerReplacePropertyStringValue(ref value, replacement, childProp);
InnerReplacePropertyStringValue(ref value, replacement);
childProp.SetValue(obj, value);
}
else
@ -83,13 +86,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
if (!currentProp.CanWrite)
continue;
string oldValue = (string)element!;
string newValue = replacement(obj!, currentProp, oldValue);
array.SetValue(newValue, i);
var oldValue = (string)element!;
var resHandler = replacement(obj!, currentProp, oldValue);
if (resHandler.Modified)
{
array.SetValue(resHandler.NewValue, i);
}
}
else if (elementType.IsClass)
{
InnerReplacePropertyStringValue(ref element, replacement, currentProp);
InnerReplacePropertyStringValue(ref element, replacement);
array.SetValue(element, i);
}
else
@ -118,13 +124,16 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
if (!currentProp.CanWrite)
continue;
string oldValue = (string)element!;
string newValue = replacement(obj, currentProp, oldValue);
list[i] = newValue;
var oldValue = (string)element!;
var resHandler = replacement(obj, currentProp, oldValue);
if (resHandler.Modified)
{
list[i] = resHandler.NewValue;
}
}
else if (elementType.IsClass)
{
InnerReplacePropertyStringValue(ref element, replacement, currentProp);
InnerReplacePropertyStringValue(ref element, replacement);
list[i] = element;
}
else
@ -154,12 +163,15 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
continue;
string oldValue = (string)entryValue!;
string newValue = replacement(obj, currentProp, oldValue);
dict[entry.Key] = newValue;
var resHandler = replacement(obj, currentProp, oldValue);
if (resHandler.Modified)
{
dict[entry.Key] = resHandler.NewValue;
}
}
else if (entryValueType.IsClass)
{
InnerReplacePropertyStringValue(ref entryValue, replacement, currentProp);
InnerReplacePropertyStringValue(ref entryValue, replacement);
dict[entry.Key] = entryValue;
}
else