feat(wxapi): 新增安全鉴权模式所需的 AES、RSA、SM2、SM4 等算法工具类

This commit is contained in:
Fu Diwei
2024-05-21 11:31:17 +08:00
committed by RHQYZ
parent 2bd10cc533
commit 7fff2b70ec
9 changed files with 1099 additions and 2 deletions

View File

@@ -0,0 +1,37 @@
using Xunit;
namespace SKIT.FlurlHttpClient.Wechat.Api.UnitTests
{
using SKIT.FlurlHttpClient.Primitives;
public class TestCase_ToolsSM4UtilityTests
{
[Fact(DisplayName = "测试用例SM4-GCM 加密")]
public void TestSM4GCMEncrypt()
{
string key = "b302fd4719dd2652";
string nonce = "b302fd4719dd";
string aad = "26523d555e6fe392b91a";
string plainText = "Awesome SKIT.FlurlHttpClient.Wechat.Api!";
string actualPlain = Utilities.SM4Utility.EncryptWithGCM(encodingKey: new EncodedString(key, EncodingKinds.Literal), encodingNonce: new EncodedString(nonce, EncodingKinds.Literal), encodingAssociatedData: new EncodedString(aad, EncodingKinds.Literal), plainData: plainText)!;
string expectedPlain = "FqW9TkELJ5soXCSksDvqNvvBIge3rRrJAc8FP8tGaQqIHx2BljkSIfXn3miUL2FBnkpBCJ96V88=";
Assert.Equal(expectedPlain, actualPlain);
}
[Fact(DisplayName = "测试用例SM4-GCM 解密")]
public void TestSM4GCMDecrypt()
{
string key = "b302fd4719dd2652";
string nonce = "b302fd4719dd";
string aad = "26523d555e6fe392b91a";
string cipherText = "FqW9TkELJ5soXCSksDvqNvvBIge3rRrJAc8FP8tGaQqIHx2BljkSIfXn3miUL2FBnkpBCJ96V88=";
string actualPlain = Utilities.SM4Utility.DecryptWithGCM(encodingKey: new EncodedString(key, EncodingKinds.Literal), encodingNonce: new EncodedString(nonce, EncodingKinds.Literal), encodingAssociatedData: new EncodedString(aad, EncodingKinds.Literal), encodingCipher: new EncodedString(cipherText, EncodingKinds.Base64))!;
string expectedPlain = "Awesome SKIT.FlurlHttpClient.Wechat.Api!";
Assert.Equal(expectedPlain, actualPlain);
}
}
}