This commit is contained in:
taoke 2022-08-26 20:32:21 +08:00
parent 75f426105f
commit a26f219b72
2 changed files with 33 additions and 0 deletions

View File

@ -3,6 +3,8 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>

View File

@ -270,6 +270,37 @@ public static class StringExtensions
}
/// <summary>
/// 对一个字符串进行sha1hash运算
/// </summary>
/// <param name="me">字符串</param>
/// <param name="e">字符串使用的编码</param>
/// <returns>hash摘要的16进制文本形式无连字符小写</returns>
public static string Sha1(this string me, Encoding e)
{
using var sha1 = new SHA1();
return BitConverter.ToString(sha1.ComputeHash(e.GetBytes(me)))
.Replace("-", string.Empty)
.ToLower(CultureInfo.CurrentCulture);
}
/// <summary>
/// 对一个字符串进行sha1hash运算
/// </summary>
/// <param name="me">字符串</param>
/// <param name="secret">密钥</param>
/// <param name="e">字符串使用的编码</param>
/// <returns>hash摘要的16进制文本形式无连字符小写</returns>
public static string HmacSha1(this string me, string secret, Encoding e)
{
using var hmacSha1 = new HMACSHA1(e.GetBytes(secret));
return BitConverter.ToString(hmacSha1.ComputeHash(e.GetBytes(me)))
.Replace("-", string.Empty)
.ToLower(CultureInfo.CurrentCulture);
}
/// <summary>
/// MD5 hmac编码
/// </summary>