本文整理汇总了C#中System.Security.Cryptography.SHA256Managed.Aggregate方法的典型用法代码示例。如果您正苦于以下问题:C# SHA256Managed.Aggregate方法的具体用法?C# SHA256Managed.Aggregate怎么用?C# SHA256Managed.Aggregate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.SHA256Managed
的用法示例。
在下文中一共展示了SHA256Managed.Aggregate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HexSha256
public static string HexSha256(string text)
{
const string salt = "jk#=¤)\"gld\"2347X#Z472\\!";
var hash = new SHA256Managed().ComputeHash(Encoding.ASCII.GetBytes(text + salt));
return hash.Aggregate("", (current, x) => current + string.Format("{0:x2}", x));
}
示例2: ToSha256
/// <summary>
/// Convert string to sha256 hash
/// </summary>
/// <param name="data">string to convert</param>
/// <returns>sha256 hash or null</returns>
public static string ToSha256(this string data)
{
try
{
if (String.IsNullOrEmpty(data))
return null;
var hashValue = new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(data));
var hex = hashValue.Aggregate("", (current, x) => current + String.Format("{0:x2}", x));
if (String.IsNullOrEmpty(hex))
throw new Exception("Erro creating SHA256 hash");
return hex;
}
catch (Exception e)
{
Debug.WriteLine(e);
return null;
}
}