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);
    }
}

 

posted on 2021-06-22 15:26  炼金师  阅读(914)  评论(0编辑  收藏  举报

导航