<blockquote>
  <p>I'm starting to wonder how difficult it is to find people who are helping others in this site</p>
</blockquote>

<p>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".</p>

<h2>My test to resolve your problem</h2>

<p>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.</p>

<p>I'm answering with C++ code.</p>

<p>Exist two types of Public key, that is compressed and uncompressed, the difference is:</p>

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

<p>But a good reference that explains the compressed public key is this <a href="https://bitcoin.stackexchange.com/questions/3059/what-is-a-compressed-bitcoin-key">What is a compressed Bitcoin key?</a> </p>

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

<p>So, my C++ code to test my idea is this:</p>

<pre><code>    cout &lt;&lt; "Test for Bitcoin.stack question" &lt;&lt; endl;
    cout &lt;&lt; "Public Key: " &lt;&lt; "02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737\n";
    string keyTmp = "02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737";
    string flag = keyTmp.substr(0, 2);
    if(flag == "02"){
          cout &lt;&lt; "Compressed with y event";
    }else if(flag == "03"){
      cout &lt;&lt; "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 &lt;&lt; "Hash Public key: " &lt;&lt; ToString(result);
</code></pre>

<p>The hash public key ofter run this code is: <code>93ce48570b55c42c2af816aeaba06cfee1224fae</code> </p>

<h2>References</h2>

<p>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.</p>

<ul>
<li><a href="https://github.com/nayuki/Bitcoin-Cryptography-Library" rel="nofollow noreferrer">Bitcoin-Cryptography-Library</a></li>
<li><a href="https://github.com/vincenzopalazzo/decompiler-bitcoin-script" rel="nofollow noreferrer">decompiler-bitcoin-script</a>: this is my start project to undestend how bitcoin script work, so you can found the idea if you want implement other feature inside your work. </li>
<li><a href="https://gist.github.com/vincenzopalazzo/b6c5112ed4e53c15c386033aad7d56e4" rel="nofollow noreferrer">Github gits code</a></li>
</ul>

CC BY-SA 4.0
2020-02-25T05:07:09.623Z
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 :)
Pretty Girl
CC BY-SA 4.0
2020-02-25T11:50:48.373Z
at the moment I'm fine, thanks, I'm happy that my solution resolved your problem. Have a good day.
vincenzopalazzo
CC BY-SA 4.0
2020-02-24T11:48:41.170Z
2020-02-24T11:55:38.370Z
2020-02-24T11:55:38.370Z
vincenzopalazzo
<p>I'm trying to hash the public key with <code>SHA256</code> and then <code>ripemed160</code> and get the hash. but it gives the wrong hash</p>

<p>Public key:  2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737</p>

<p>Hash should be: 93ce48570b55c42c2af816aeaba06cfee1224fae</p>

<pre><code>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
</code></pre>

<p>and is there any way I can shorten this code? loops &amp; if statements etc</p>

CC BY-SA 4.0
2020-02-23T11:58:57.190Z
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.
Raghav Sood
CC BY-SA 4.0
2020-02-23T12:08:53.587Z
@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
Pretty Girl
CC BY-SA 4.0
2020-02-23T12:15:39.000Z
where do I post bitcoin related code questions?
Pretty Girl
CC BY-SA 4.0
2020-02-23T12:24:01.670Z
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
vincenzopalazzo
CC BY-SA 4.0
2020-02-23T12:30:29.420Z
@vincenzopalazzo changed the title
Pretty Girl
CC BY-SA 4.0
2020-02-23T13:08:14.747Z
Before try to understand your code you want to see how bitcoin create the hex from string, look the method HexStr https://github.com/bitcoin/bitcoin/blob/ab9de435880c9d77e4137b65050591ef2d14f809/src/util/strencodings.h#L125
vincenzopalazzo
CC BY-SA 4.0
2020-02-23T14:22:32.413Z
@vincenzopalazzo code updated
Pretty Girl
CC BY-SA 4.0
2020-02-23T15:07:09.820Z
I swear I've seen this (sort of strange) code example before.
Claris
CC BY-SA 4.0
2020-02-24T07:55:28.167Z
I'm starting to wonder how difficult it is to find people who are helping others in this site.
Pretty Girl
<hash><sha256><ripemd160>
How to generate the hash of public key in Bitcoin
Pretty Girl
# How to generate the hash of public key in Bitcoin

(Asked by: username_0 on 2020-02-23)

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

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

**Tags:** hash, sha256, ripemd160

> **Comments:**
> **username_1**: 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.  
> **username_0**: @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  
> **username_0**: where do I post bitcoin related code questions?  
> **username_2**: 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  
> **username_0**: @username_2 changed the title  
> **username_2**: Before try to understand your code you want to see how bitcoin create the hex from string, look the method HexStr https://github.com/bitcoin/bitcoin/blob/ab9de435880c9d77e4137b65050591ef2d14f809/src/util/strencodings.h#L125  
> **username_0**: @username_2 code updated  
> **username_3**: I swear I've seen this (sort of strange) code example before.  
> **username_0**: I'm starting to wonder how difficult it is to find people who are helping others in this site.  

---

## 1 answers

### Answer by username_2 on 2020-02-24. Score: 0 | Accepted answer

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

The hash public key ofter run this code is: 93ce48570b55c42c2af816aeaba06cfee1224fae 
References
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.

Bitcoin-Cryptography-Library
decompiler-bitcoin-script: this is my start project to undestend how bitcoin script work, so you can found the idea if you want implement other feature inside your work. 
Github gits code

> **Comments:**
> **username_0**: 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 :)  
> **username_2**: at the moment I'm fine, thanks, I'm happy that my solution resolved your problem. Have a good day.  

---

