-
Notifications
You must be signed in to change notification settings - Fork 1
/
VerifySignDefaultFilter.cs
54 lines (48 loc) · 1.75 KB
/
VerifySignDefaultFilter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Microsoft.Extensions.Configuration;
namespace NetPro.Sign
{
public class VerifySignDefaultFilter : IOperationFilter
{
private readonly IConfiguration _configuration;
public VerifySignDefaultFilter(IConfiguration configuration)
{
_configuration = configuration;
}
/// <summary>
/// 获取appid对应的secret
/// </summary>
/// <param name="appid"></param>
/// <returns></returns>
public virtual string GetSignSecret(string appid)
{
var secret = _configuration.GetValue<string>($"{nameof(VerifySignOption)}:AppSecret:AppId:{appid}");
return secret;
}
/// <summary>
/// 签名算法;支持hmac-sha256;md5
/// </summary>
/// <param name="message"></param>
/// <param name="secret"></param>
/// <param name="signMethod">哈希算法,默认HMACSHA256</param>
/// <returns>签名16进制</returns>
public virtual string GetSignhHash(string message, string secret, EncryptEnum signMethod = EncryptEnum.Default)
{
if (signMethod.HasFlag(EncryptEnum.Default) || signMethod.HasFlag(EncryptEnum.SignHMACSHA256))
{
return SignCommon.GetHMACSHA256Sign(message, secret);
}
else if (signMethod.HasFlag(EncryptEnum.SignSHA256))
{
return SignCommon.GetSHA256Sign(message, secret);
}
else if (signMethod.HasFlag(EncryptEnum.SignMD5))
{
return SignCommon.CreateMD5(message, secret);
}
else
{
return SignCommon.GetHMACSHA256Sign(message, secret);
}
}
}
}