如何对SHA-256哈希进行十六进制编码 [英] How to Hex Encode a SHA-256 Hash

查看:1269
本文介绍了如何对SHA-256哈希进行十六进制编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中正确对SHA-256哈希进行十六进制编码?

How to Hex Encode a SHA-256 hash properly in C#?

private static string ToHex(byte[] bytes, bool upperCase)
{
    StringBuilder result = new StringBuilder(bytes.Length * 2);

    for (int i = 0; i < bytes.Length; i++)
        result.Append(bytes[i].ToString(upperCase ? "X2" : "x2"));

    return result.ToString();
}

private string hashRequestBody(string reqBody)
{
    string hashString;
    using (var sha256 = SHA256Managed.Create())
    {
        var hash = sha256.ComputeHash(Encoding.Default.GetBytes(reqBody));
        hashString = ToHex(hash, false);
    }

    MessageBox.Show(hashString);
    return hashString;
}

我这样做了,但是结果与我使用的银行的沙盒不同。

I did this, but the result is different with bank's sandbox I worked with.

测试数据:

{ CorporateID: BCAAPI2016, SourceAccountNumber : 0201245680, TransactionID: 00000001, TransactionDate: 2017-09-13, ReferenceID: refID, CurrencyCode: IDR, Amount: 10000, BeneficiaryAccountNumber: 0201245681, Remark1:转移测试, Remark2:在线转移}

银行的沙箱结果: e9d06986c1ed6b063bf59aa873030013725c518631deef2b2147e614017c2141

矿山: 1c83acc42cf905ca8afba27ef0640c70ad2856a366b57c >

Mine: 1c83acc42cf905ca8afba27ef0640c70ad2856a366b57c17cf16f2894327676e

推荐答案

我已经看到了解决此问题的几种方法,但是您的代码是最优雅的。我对其进行了一些重构,并对其进行了测试。我还得到了哈希值:

I've seen several solutions to this problem, but your code is the most elegant. I slightly re-factored it and tested it for this answer. I also get the hash:


1c83acc42cf905ca8afba27ef0640c70ad2856a366b57c17cf16f2894327676e

1c83acc42cf905ca8afba27ef0640c70ad2856a366b57c17cf16f2894327676e

在此处查看正在工作的小提琴: https://dotnetfiddle.net/QbsKTc

See working fiddle here: https://dotnetfiddle.net/QbsKTc

也许此哈希值与银行的哈希值不同,因为您更改了JSON字符串以删除私人数据?

Perhaps this hash is different to the bank's because you changed the JSON string to remove private data?

using System;
using System.Security.Cryptography;
using System.Text;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(SHA256HexHashString("{\"CorporateID\":\"BCAAPI2016\",\"SourceAccountNumber\":\"0201245680\",\"TransactionID\":\"00000001\",\"TransactionDate\":\"2017-09-13\",\"ReferenceID\":\"refID\",\"CurrencyCode\":\"IDR\",\"Amount\":\"10000\",\"BeneficiaryAccountNumber\":\"0201245681\",\"Remark1\":\"Transfer Test\",\"Remark2\":\"Online Transfer\"}"));
    }

    private static string ToHex(byte[] bytes, bool upperCase)
    {
        StringBuilder result = new StringBuilder(bytes.Length * 2);
        for (int i = 0; i < bytes.Length; i++)
            result.Append(bytes[i].ToString(upperCase ? "X2" : "x2"));
        return result.ToString();
    }

    private static string SHA256HexHashString(string StringIn)
    {
        string hashString;
        using (var sha256 = SHA256Managed.Create())
        {
            var hash = sha256.ComputeHash(Encoding.Default.GetBytes(StringIn));
            hashString = ToHex(hash, false);
        }

        return hashString;
    }
}

这篇关于如何对SHA-256哈希进行十六进制编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆