<p>You should probably include your non-working Python code so that Pythonistas can identify where you are going wrong.</p>
<p>A common mistake is not first converting a (human readable) hex string into a (computer native) byte array.</p>
<p>Your theory is sound ...</p>
<p>Output:</p>
<pre><code>1558c7cd9825447a31990ff964f347bb2dbfe9be
</code></pre>
<p>Source code in <a href="https://go.dev/" rel="nofollow noreferrer">go</a>:</p>
<pre class="lang-go prettyprint-override"><code>package main

import (
    &quot;crypto/sha256&quot;
    &quot;encoding/hex&quot;
    &quot;fmt&quot;

    &quot;golang.org/x/crypto/ripemd160&quot;
)

func main() {

    pubKeyHex := &quot;04ce0ed35340803b0c21f2f7f5d5ab9d687e5fa95a79471c9b5c9d97a0bb170eac1045230cc51d13b85a5f64feb80f8fc19358a396797926e3f89d49066b1abc07&quot;
    pubKey, err := hex.DecodeString(pubKeyHex)
    check(err)

    hasher256 := sha256.New()
    hasher256.Write(pubKey)
    hash256 := hasher256.Sum(nil)

    hasher160 := ripemd160.New()
    hasher160.Write(hash256)
    hash160 := hasher160.Sum(nil)

    fmt.Printf(&quot;%x&quot;, hash160)
}

func check(err error) {
    if err != nil {
        panic(err)
    }
}
</code></pre>
<p>See that code running at <a href="https://go.dev/play/p/wNZzCv6l3tU" rel="nofollow noreferrer">https://go.dev/play/p/wNZzCv6l3tU</a></p>

CC BY-SA 4.0
2022-11-08T00:11:55.570Z
thanks—seeing this code made me realize my mistake... needs to be bytes into sha256 and ripemd. Guess you solved it for me, appreciate it
FKAjasmine
CC BY-SA 4.0
2022-11-07T22:24:29.673Z
2022-11-30T11:52:11.453Z
2022-11-30T11:52:11.453Z
RedGrittyBrick
<p>I have a dumb question about Hash160 (specifically, going from a public key to Hash160).</p>
<p>If I take a public key: 04ce0ed35340803b0c21f2f7f5d5ab9d687e5fa95a79471c9b5c9d97a0bb170eac1045230cc51d13b85a5f64feb80f8fc19358a396797926e3f89d49066b1abc07</p>
<p>and I run it through a hash160 calculator (<a href="https://www.btcschools.net/bitcoin/bitcoin_tool_hash160.php" rel="nofollow noreferrer">https://www.btcschools.net/bitcoin/bitcoin_tool_hash160.php</a>), I get a Hash160 of: 1558c7cd9825447a31990ff964f347bb2dbfe9be</p>
<p>This is the correct Hash160 (according to other trusted sources).</p>
<p>I'm trying to go through the Hash160 steps manually (just to learn). My understanding is that Hash160 is just running the public key through SHA256, and then running that output through RIPEMD160. However, when I try to recreate the correct Hash160 output by running that public key through SHA256 and then RIPEMD160 (say, using online calculators, although I've tried on other calculators and via python so I could control binary/hex encoding), I get a SHA256 output of: a5d0a142f10031f9e2d3f806f4845005cd5b3b2722c335d5a352c268a0ee1ec9. Then, when I run that through RIPEMD160, I get other outputs (depending on what is going into the RIPEMD, like if it's hex or binary or whatever).</p>
<p>But none of them are the Hash160 output I was expecting. I'm guessing there's some step I'm missing... do I need to do anything to the public key first? Or do something after running it through SHA256 or RIPEMD160 or something?</p>
<p>Just really at a loss as to why literally every source I can find says that Hash160 is just RIPEMD160(SHA256( publicKey )) but that path doesn't seem to work when attempting to do this manually.</p>
<p>I found this post (<a href="https://bitcoin.stackexchange.com/questions/114293/op-hash160-computing-script-hash-in-a-concrete-p2sh-transaction">OP_HASH160: Computing script hash in a concrete P2SH transaction</a>) but I've tried all of the binary and hex permutations via the python hashlib that I could think of (tried all binary in, all hash in, some hash in some binary in, etc.)</p>
<p>Any help would be amazing, thanks!!</p>

<hash><public-key><sha256><ripemd160>
replicating Hash160 with SHA256/RIPEMD160
FKAjasmine
# replicating Hash160 with SHA256/RIPEMD160

(Asked by: username_0 on 2022-11-07)

I have a dumb question about Hash160 (specifically, going from a public key to Hash160).

If I take a public key: 04ce0ed35340803b0c21f2f7f5d5ab9d687e5fa95a79471c9b5c9d97a0bb170eac1045230cc51d13b85a5f64feb80f8fc19358a396797926e3f89d49066b1abc07

and I run it through a hash160 calculator (<https://www.btcschools.net/bitcoin/bitcoin_tool_hash160.php>), I get a Hash160 of: 1558c7cd9825447a31990ff964f347bb2dbfe9be

This is the correct Hash160 (according to other trusted sources).

I'm trying to go through the Hash160 steps manually (just to learn). My understanding is that Hash160 is just running the public key through SHA256, and then running that output through RIPEMD160. However, when I try to recreate the correct Hash160 output by running that public key through SHA256 and then RIPEMD160 (say, using online calculators, although I've tried on other calculators and via python so I could control binary/hex encoding), I get a SHA256 output of: a5d0a142f10031f9e2d3f806f4845005cd5b3b2722c335d5a352c268a0ee1ec9. Then, when I run that through RIPEMD160, I get other outputs (depending on what is going into the RIPEMD, like if it's hex or binary or whatever).

But none of them are the Hash160 output I was expecting. I'm guessing there's some step I'm missing... do I need to do anything to the public key first? Or do something after running it through SHA256 or RIPEMD160 or something?

Just really at a loss as to why literally every source I can find says that Hash160 is just RIPEMD160(SHA256( publicKey )) but that path doesn't seem to work when attempting to do this manually.

I found this post ([OP\_HASH160: Computing script hash in a concrete P2SH transaction](https://bitcoin.stackexchange.com/questions/114293/op-hash160-computing-script-hash-in-a-concrete-p2sh-transaction)) but I've tried all of the binary and hex permutations via the python hashlib that I could think of (tried all binary in, all hash in, some hash in some binary in, etc.)

Any help would be amazing, thanks!!

**Tags:** hash, public-key, sha256, ripemd160


---

## 1 answers

### Answer by username_1 on 2022-11-07. Score: 0 | Accepted answer

You should probably include your non-working Python code so that Pythonistas can identify where you are going wrong.

A common mistake is not first converting a (human readable) hex string into a (computer native) byte array.

Your theory is sound ...

Output:

```
1558c7cd9825447a31990ff964f347bb2dbfe9be

```

Source code in [go](https://go.dev/):

```go
package main

import (
    "crypto/sha256"
    "encoding/hex"
    "fmt"

    "golang.org/x/crypto/ripemd160"
)

func main() {

    pubKeyHex := "04ce0ed35340803b0c21f2f7f5d5ab9d687e5fa95a79471c9b5c9d97a0bb170eac1045230cc51d13b85a5f64feb80f8fc19358a396797926e3f89d49066b1abc07"
    pubKey, err := hex.DecodeString(pubKeyHex)
    check(err)

    hasher256 := sha256.New()
    hasher256.Write(pubKey)
    hash256 := hasher256.Sum(nil)

    hasher160 := ripemd160.New()
    hasher160.Write(hash256)
    hash160 := hasher160.Sum(nil)

    fmt.Printf("%x", hash160)
}

func check(err error) {
    if err != nil {
        panic(err)
    }
}

```

See that code running at <https://go.dev/play/p/wNZzCv6l3tU>

> **Comments:**
> **username_0**: thanks—seeing this code made me realize my mistake... needs to be bytes into sha256 and ripemd. Guess you solved it for me, appreciate it  

---

