1

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

1 Answer 1

0

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:

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

1
  • 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
    Commented Nov 8, 2022 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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