mirror of
https://github.com/ldqk/Masuit.Tools.git
synced 2026-10-28 11:47:11 +08:00
66 lines
1.9 KiB
C#
66 lines
1.9 KiB
C#
using Masuit.Tools.Security;
|
|
using System;
|
|
using Masuit.Tools.Extension.Object;
|
|
using Xunit;
|
|
|
|
namespace Masuit.Tools.Abstractions.Test.Security
|
|
{
|
|
public class RsaCryptTest
|
|
{
|
|
public class RsaCryptTestEntity
|
|
{
|
|
public string Name { get; set; }
|
|
|
|
public DateTime SdTime { get; set; }
|
|
}
|
|
|
|
[Fact]
|
|
public void RsaCryptWordTestWithNoException()
|
|
{
|
|
var rsaKey = RsaCrypt.GenerateRsaKeys();
|
|
|
|
string result = new RsaCryptTestEntity()
|
|
{
|
|
SdTime = DateTime.Parse("2020-08-14"),
|
|
Name = "asdf"
|
|
}
|
|
.ToJsonString()
|
|
.RSAEncrypt()
|
|
.RSADecrypt();
|
|
|
|
Assert.Equal(result, new RsaCryptTestEntity()
|
|
{
|
|
SdTime = DateTime.Parse("2020-08-14"),
|
|
Name = "asdf"
|
|
}
|
|
.ToJsonString());
|
|
}
|
|
|
|
[Fact]
|
|
public void 签名验证()
|
|
{
|
|
RsaKey rsaKey = RsaCrypt.GenerateRsaKeys();
|
|
string data = "Hello World!".Base64Encrypt();
|
|
string sign = data.SignatureString(rsaKey.PrivateKey);
|
|
|
|
// 验证结果为False
|
|
Assert.True(data.SignatureDeformatter(rsaKey.PublicKey, sign));
|
|
}
|
|
|
|
[Fact]
|
|
public void GenerateRsaKeys_ShouldRejectWeakKeySizes()
|
|
{
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => RsaCrypt.GenerateRsaKeys(length: 1024));
|
|
}
|
|
|
|
[Fact]
|
|
public void RsaSignatures_ShouldRejectWeakHashAlgorithms()
|
|
{
|
|
RsaKey rsaKey = RsaCrypt.GenerateRsaKeys();
|
|
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => "data".SignatureString(rsaKey.PrivateKey, HashAlgo.MD5));
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => "data".SignatureString(rsaKey.PrivateKey, HashAlgo.SHA1));
|
|
}
|
|
}
|
|
}
|