mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-04-23 04:29:36 +08:00
修改扩展方法
This commit is contained in:
parent
cac17aa6c2
commit
b233dbcc5d
35
Api/Utility/DataExtension.cs
Normal file
35
Api/Utility/DataExtension.cs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
namespace Milimoe.FunGame.Core.Api.Utility
|
||||||
|
{
|
||||||
|
public static class DictionaryExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 从 <see cref="Dictionary{String, Object}"/> 中获取指定键的值,并将其转换为指定的类型。
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">要转换的目标类型</typeparam>
|
||||||
|
/// <param name="dict">要从中获取值的字典</param>
|
||||||
|
/// <param name="key">要获取值的键</param>
|
||||||
|
/// <returns>转换后的值,如果键不存在或转换失败,则返回默认值。</returns>
|
||||||
|
public static T? GetValue<T>(this Dictionary<string, object> dict, string key)
|
||||||
|
{
|
||||||
|
if (dict is null || !dict.TryGetValue(key, out object? value) || value is null)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 如果已经是目标类型,则直接返回
|
||||||
|
if (value is T t)
|
||||||
|
{
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NetworkUtility.JsonDeserializeFromDictionary<T>(dict, key);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -491,24 +491,6 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 为字符串(string)添加扩展方法
|
|
||||||
/// </summary>
|
|
||||||
public static class StringExtension
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 使用 HMAC-SHA512 算法对文本进行加密<para/>
|
|
||||||
/// 注意:此方法会先将 <paramref name="key" /> 转为小写并计算两次哈希。
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="text">需要加密的文本</param>
|
|
||||||
/// <param name="key">用于加密的秘钥</param>
|
|
||||||
/// <returns>加密后的 HMAC-SHA512 哈希值</returns>
|
|
||||||
public static string Encrypt(this string text, string key)
|
|
||||||
{
|
|
||||||
return Encryption.HmacSha512(text, Encryption.HmacSha512(text, key.ToLower()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 验证服务
|
#region 验证服务
|
||||||
|
20
Api/Utility/StringExtension.cs
Normal file
20
Api/Utility/StringExtension.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
namespace Milimoe.FunGame.Core.Api.Utility
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 为字符串(string)添加扩展方法
|
||||||
|
/// </summary>
|
||||||
|
public static class StringExtension
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 使用 HMAC-SHA512 算法对文本进行加密<para/>
|
||||||
|
/// 注意:此方法会先将 <paramref name="key" /> 转为小写并计算两次哈希。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">需要加密的文本</param>
|
||||||
|
/// <param name="key">用于加密的秘钥</param>
|
||||||
|
/// <returns>加密后的 HMAC-SHA512 哈希值</returns>
|
||||||
|
public static string Encrypt(this string text, string key)
|
||||||
|
{
|
||||||
|
return Encryption.HmacSha512(text, Encryption.HmacSha512(text, key.ToLower()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user