Hash

如何在比特幣中生成公鑰的雜湊

  • February 24, 2020

SHA256我正在嘗試使用然後對公鑰進行散列ripemed160並獲取散列。但它給出了錯誤的雜湊

公鑰:2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737

雜湊應該是:93ce48570b55c42c2af816aeaba06cfee1224fae

Imports System.Security.Cryptography
Imports System.Text
Imports System.Globalization
Imports System.Numerics

Private Function HexStringToByteArray(ByVal shex As String) As Byte()
   Dim B As Byte() = Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
   Return Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
End Function


Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click

Dim public_key as String = "2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737"
  Dim pubkey_hash As String

   Dim sha256 As SHA256 = SHA256Managed.Create()
   Dim bytes As Byte() = System.Text.Encoding.Unicode.GetBytes(public_key)
   Dim hash As Byte() = sha256.ComputeHash(bytes)
   Dim stringBuilder1 As New StringBuilder()

   For i As Integer = 0 To hash.Length - 1
       stringBuilder1.Append(hash(i).ToString("X2"))
   Next

   bytes = HexStringToByteArray(stringBuilder1.ToString())

   Dim ripemd160 As RIPEMD160 = RIPEMD160Managed.Create()
   Dim hash_160 As Byte() = ripemd160.ComputeHash(bytes)

   Dim stringBuilder2 As New StringBuilder()

   For i As Integer = 0 To hash_160.Length - 1
       stringBuilder2.Append(hash_160(i).ToString("X2"))
   Next

   Dim arr() As Char = stringBuilder2.ToString 

   pubkey_hash = arr.tostring
   TextBox14.Text = pubkey_hash


  End Sub

有什麼辦法可以縮短這段程式碼嗎?循環和 if 語句等

我開始懷疑在這個網站上找到幫助他人的人有多難

我沒有你同樣的想法,但很抱歉延遲回答,但在意大利目前存在警報“冠狀病毒”。

我的測試來解決你的問題

我不知道您在問題中公開的程式碼的語言,但我有一個反編譯腳本比特幣的項目,但處於啟動階段。

我用 C++ 程式碼回答。

存在兩種類型的公鑰,即壓縮的和未壓縮的,區別在於:

  • 壓縮的公鑰以標誌“02”開頭(我認為這是你的情況)
  • 以標誌“03”開始的未壓縮
  • 如果以“04”開頭的密鑰不是公鑰,而是 P2PK 腳本。

但是解釋壓縮公鑰的一個很好的參考是什麼是壓縮比特幣密鑰?

所以,我認為你的錯誤是公鑰輸入,你的公鑰是02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737不是2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737,你失去了壓縮公鑰的標誌部分。

所以,我的 C++ 程式碼來測試我的想法是這樣的:

   cout << "Test for Bitcoin.stack question" << endl;
   cout << "Public Key: " << "02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737\n";
   string keyTmp = "02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737";
   string flag = keyTmp.substr(0, 2);
   if(flag == "02"){
         cout << "Compressed with y event";
   }else if(flag == "03"){
     cout << "Compressed with y odd";
   }else{
     assert(flag == "04");
   }
   Bytes bytesTmp = hexBytes(keyTmp.c_str());

   //SHA256
   Sha256Hash shaHash = Sha256::getHash(bytesTmp.data(), bytesTmp.size());
   uint8_t result[Ripemd160::HASH_LEN];
   Ripemd160::getHash(shaHash.value, sizeof(shaHash), result);

   cout << "Hash Public key: " << ToString(result);

執行這段程式碼的雜湊公鑰是:93ce48570b55c42c2af816aeaba06cfee1224fae

參考

該項目的參考在這裡是題外話,但我認為沒有它,這篇文章可能是假的,所以我在這個答案中添加了用於實現我的程式碼的庫的參考。

引用自:https://bitcoin.stackexchange.com/questions/93437