我想按照說明衍生一個十六進制比特幣私鑰的WIF從 - https://en.bitcoin.it/wiki/Wallet_import_format 然而,當我嘗試散列字符串(包括0x80字節),我得到不同結果比預期的要好。 我應該得到8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592。 而是我收到e2e4146a36e9c455cf95a4f259f162c353cd419cc3fd0e69ae36d7d1b6cd2c09。比特幣sha256到十六進制產生不同的結果比預期的
我在谷歌上廣泛閱讀,我瞭解到我應該將字符串轉換爲二進制文件。我做了,然後將這些二進制文件中的char數組散列爲相同的結果。
代碼現在正得益於@Heinan Cabouly和@JaredPar
這裏是工作代碼:
using System;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
namespace Base58Encode
{
internal class Program
{
public static string Str = "800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D";
public static byte[] Bytes;
public static void Main()
{
Bytes = StringToByteArray(Str);
SHA256Managed sha = new SHA256Managed();
string hashstr = String.Empty;
byte[] encrypt = sha.ComputeHash(Bytes);
foreach (byte b in encrypt)
{
hashstr += b.ToString("x2");
}
Console.WriteLine(hashstr);
//prints e2e4146a36e9c455cf95a4f259f162c353cd419cc3fd0e69ae36d7d1b6cd2c09
//instead of 8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592
Console.ReadLine();
}
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
}
}
這是怎麼湊在C#中的十六進制字符串SHA-256。 謝謝大家!幫助我出去了很多!
'byte [] ba = Encoding.ASCII.GetBytes(Str);' - 你知道這不是將字符串解析爲十六進制,對吧?它只是使用ASCII編碼將字符串轉換爲字節,因此每個字符一個字節。在堆棧溢出搜索「解析十六進制字符串到字節C#」... –
謝謝,我想通了。 :) – user3410566