代碼地址: "DotNetCore_Practice/JavaToC /DigestUtils at master · PuzzledAlien/DotNetCore_Practice" ...
代碼地址:DotNetCore_Practice/JavaToC#/DigestUtils at master · PuzzledAlien/DotNetCore_Practice
public class Hex
{
/// <summary>
/// 位元組數組轉換為Hex字元串
/// </summary>
/// <param name="data"></param>
/// <param name="toLowerCase"></param>
/// <returns></returns>
public static string ByteArrayToHexString(byte[] data, bool toLowerCase = true)
{
var hex = BitConverter.ToString(data).Replace("-", string.Empty);
return toLowerCase ? hex.ToLower() : hex.ToUpper();
}
}
public class DigestUtils
{
/// <summary>
/// SHA256 轉換為 Hex字元串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string Sha256Hex(string data)
{
var bytes = Encoding.UTF8.GetBytes(data);
using (var sha256 = SHA256.Create())
{
var hash = sha256.ComputeHash(bytes);
return Hex.ByteArrayToHexString(hash);
}
}
/// <summary>
/// SHA256 轉換為 Hex字元串
/// </summary>
/// <param name="data"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string Sha256Hex(string data, Encoding encoding)
{
var bytes = encoding.GetBytes(data);
using (var sha256 = SHA256.Create())
{
var hash = sha256.ComputeHash(bytes);
return Hex.ByteArrayToHexString();
}
}
/// <summary>
/// SHA256 轉換為 Hex字元串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string Sha256Hex(byte[] bytes)
{
using (var sha256 = SHA256.Create())
{
var hash = sha256.ComputeHash(bytes);
return Hex.ByteArrayToHexString();
}
}
}