c# sha256hex

/// <summary>
/// 字节数组转换为Hex字符串
/// </summary>
/// <param name="data"></param>
/// <param name="toLowerCase"></param>
/// <returns></returns>
private static string ByteArrayToHexString(byte[] data, bool toLowerCase = true)
{
    var hex = BitConverter.ToString(data).Replace("-", string.Empty);
    return toLowerCase ? hex.ToLower() : hex.ToUpper();
}

/// <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 ByteArrayToHexString(hash);
    }
}

 

上一篇:idea控制台日志中文乱码


下一篇:Redis数据结构必知必会