I'm trying to hash the public key with SHA256 and then ripemed160 and get the hash. but it gives the wrong hash

Public key: 2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737

Hash should be: 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

    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

    Dim arr() As Char = stringBuilder2.ToString 

    pubkey_hash = arr.tostring
    TextBox14.Text = pubkey_hash

   End Sub

and is there any way I can shorten this code? loops & if statements etc

  • 5
    I'm voting to close this question as off-topic because it is a general programming error specific to the language use, and unrelated to bitcoin apart from the fact that the data being processed is bitcoin keys. Commented Feb 23, 2020 at 11:58
  • @RaghavSood, it is related to bitcoin of course, it's the generation of the public key hashing of bitcoin to be used inside a transaction Commented Feb 23, 2020 at 12:08
  • where do I post bitcoin related code questions? Commented Feb 23, 2020 at 12:15
  • You can post here the code related to bitcoin code, but I think your title question should be "How to generate the hash of public key in Bitcoin'" and after you can explain your error Commented Feb 23, 2020 at 12:24
  • @vincenzopalazzo changed the title Commented Feb 23, 2020 at 12:30

1 Answer 1


I'm starting to wonder how difficult it is to find people who are helping others in this site

I don't have your same idea, but sorry for the delay in the answer but in Italy exist at the moment the alert "coronavirus".

My test to resolve your problem

I don't know the language of the code that you have public in the question but I have a project to decompiler script bitcoin but is in the start phases.

I'm answering with C++ code.

Exist two types of Public key, that is compressed and uncompressed, the difference is:

  • Compressed public key started with a flag "02" (I think this is your cases)
  • Uncompressed start with the flag "03"
  • If the you key start with "04" doesn't is the public key but is a P2PK script.

But a good reference that explains the compressed public key is this What is a compressed Bitcoin key?

So, I think your error is the public key input,your public key is 02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737 and not 2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737, you have lose the the part of flag of the compressed public key.

So, my C++ code to test my idea is this:

    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";
      assert(flag == "04");
    Bytes bytesTmp = hexBytes(keyTmp.c_str());

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

The hash public key ofter run this code is: 93ce48570b55c42c2af816aeaba06cfee1224fae


The reference of the project are off-topics here but I think without it this post can be fake, so I'm adding the reference of the library used to implement my code in this answer.

  • 1
    wow, you solved it, thanks a lot, it was lacking the 'zero' infront of the key which it should have started as 02. :) , hope you'll be fine with the coronavirus you're facing at the moment :) Commented Feb 25, 2020 at 5:07
  • at the moment I'm fine, thanks, I'm happy that my solution resolved your problem. Have a good day. Commented Feb 25, 2020 at 11:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.