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();
}
}
}
C#实现Java的DigestUtils.sha256Hex
最新推荐文章于 2024-07-27 15:17:23 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)