<p>In Python3:</p>



<pre><code>#!/usr/bin/env python3

import hashlib
import codecs

publickey = codecs.decode('025914AE6A4B33A9EB1AF17FB818556F5FC849A2B9AAF176C48983BABD75A76432', 'hex')
s = hashlib.new('sha256', publickey).digest()
r = hashlib.new('ripemd160', s).digest()

print(codecs.encode(s, 'hex').decode("utf-8"))
print(codecs.encode(r, 'hex').decode("utf-8"))
</code></pre>

<p>Output:</p>



<pre><code>c8bf4c378130fc18fcf732051e95fce4646771639097ea19b59fb5d005bf3998
cb6af4b9e44a21f9767e8989c4413c417a8b8511
</code></pre>

<p>A greeting.</p>

CC BY-SA 4.0
2019-06-13T13:57:07.073Z
2019-06-13T13:57:07.073Z
JavDomGom
<p>The hex string should be transaformed to bytes first.<br>
Change the string to <br></p>

<pre><code>b'02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231'
</code></pre>

<p>Then you should get the expected result~ <br></p>

<p>Here is a relevant post
<a href="https://bitcoin.stackexchange.com/questions/36802/about-performing-sha-256-hashing-on-the-public-key">About performing SHA-256 hashing on the public key</a></p>

CC BY-SA 3.0
2015-04-23T05:13:01.197Z
This is for Python 3.x FWIW, Python 2.x doesn't distinguish between byte strings and strings, but the main thing is to be sure the hex chars are run through `binascii.unhexlify` first
Wizard Of Ozzie
CC BY-SA 3.0
2015-04-22T13:05:41.003Z
2015-04-22T13:05:41.003Z
2017-04-13T12:47:33.583Z
moshaholo
<p>moshaholo has the right answer, but not quite the right code.</p>

<pre><code>&gt;&gt;&gt; publickey = '02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231'.decode('hex')
&gt;&gt;&gt; s = hashlib.new('sha256',    publickey).digest()
&gt;&gt;&gt; r = hashlib.new('ripemd160', s        ).digest()
&gt;&gt;&gt; s.encode('hex')
'ea571f53cb3a9865d3dc74735e0c16643d319c6ad81e199b9c8408cecbcec7bb'
&gt;&gt;&gt; r.encode('hex')
'5238c71458e464d9ff90299abca4a1d7b9cb76ab'
</code></pre>

<p>Notice that <code>publickey</code>, <code>s</code>, and <code>r</code> are all binary byte strings (because <code>.digest()</code> is being used instead of <code>.hexdigest()</code>). The only reason to convert them to hex-encoded strings is for display purposes. You can display them directly if you like, but it's not pretty....</p>

<pre><code>&gt;&gt;&gt; publickey
'\x02!\x8a\xd6\xcd\xc62\xe7\xae}\x04G#t1\x1c\xeb\xbb\xbf\n\xb5@\xd2\xd0\x8c4\x00\xbb\x84LeB1'
</code></pre>

CC BY-SA 3.0
2015-04-22T14:01:05.940Z
2015-04-22T15:01:01.050Z
2015-04-22T15:01:01.050Z
Christopher Gurnee
<p><em>(Note: I'm using <a href="http://www.royalforkblog.com/2014/08/11/graphical-address-generator/" rel="noreferrer">http://www.royalforkblog.com/2014/08/11/graphical-address-generator/</a> (use "bitcoin" as the phrase for generating the private key)</em></p>

<p><strong>I'm trying to take the <em>first step</em> in going from a publickey to an address.</strong></p>

<p>For example, if this is my public key:</p>

<pre><code>publickey = 02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231
</code></pre>

<p>This should be the result of the next step:</p>

<pre><code>ripemd160(sha256(publickey)) = 5238C71458E464D9FF90299ABCA4A1D7B9CB76AB
</code></pre>

<p>However, when I try to do this in python, I fail to get the same result:</p>

<pre><code>&gt;&gt;&gt; hashlib.new('sha256', '02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231').hexdigest()
'6e28eefe40e5855ed8cdf0599ec12e52f73a5b222c84ba957b4dcdd2d8a9cf2a'

&gt;&gt;&gt; hashlib.new('ripemd160', '6e28eefe40e5855ed8cdf0599ec12e52f73a5b222c84ba957b4dcdd2d8a9cf2a').hexdigest()
'e607ff4fa71ef33ca357415e3d2f781c4cedacc3'
</code></pre>

<p>Where am I going wrong when using sha256 and ripemd160 in my python code?</p>

CC BY-SA 3.0
2015-04-22T12:46:47.557Z
Furthermore, I cannot recreate the hash results using SHA256 or RIPEMD160 from the steps outlined on the Bitcoin wiki either: https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses
inersha
CC BY-SA 3.0
2015-04-25T11:48:54.330Z
inersha, if any response below has completely answered your question, please consider Accepting it. If not, please consider commenting on why it is incomplete so that it can be improved. More info on Accepting answers can be found here: http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work#5235
Christopher Gurnee
<public-key>
ripemd160(sha256(publickey)) - Where am I going wrong?
inersha
# ripemd160(sha256(publickey)) - Where am I going wrong?

(Asked by: username_0 on 2015-04-22)

*(Note: I'm using <http://www.royalforkblog.com/2014/08/11/graphical-address-generator/> (use "bitcoin" as the phrase for generating the private key)*

**I'm trying to take the *first step* in going from a publickey to an address.**

For example, if this is my public key:

```
publickey = 02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231

```

This should be the result of the next step:

```
ripemd160(sha256(publickey)) = 5238C71458E464D9FF90299ABCA4A1D7B9CB76AB

```

However, when I try to do this in python, I fail to get the same result:

```
>>> hashlib.new('sha256', '02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231').hexdigest()
'6e28eefe40e5855ed8cdf0599ec12e52f73a5b222c84ba957b4dcdd2d8a9cf2a'

>>> hashlib.new('ripemd160', '6e28eefe40e5855ed8cdf0599ec12e52f73a5b222c84ba957b4dcdd2d8a9cf2a').hexdigest()
'e607ff4fa71ef33ca357415e3d2f781c4cedacc3'

```

Where am I going wrong when using sha256 and ripemd160 in my python code?

**Tags:** public-key

> **Comments:**
> **username_0**: Furthermore, I cannot recreate the hash results using SHA256 or RIPEMD160 from the steps outlined on the Bitcoin wiki either: https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses  
> **username_1**: username_0, if any response below has completely answered your question, please consider Accepting it. If not, please consider commenting on why it is incomplete so that it can be improved. More info on Accepting answers can be found here: http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work#5235  

---

## 3 answers

### Answer by username_1 on 2015-04-22. Score: 6 | Accepted answer

username_3 has the right answer, but not quite the right code.

```
>>> publickey = '02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231'.decode('hex')
>>> s = hashlib.new('sha256',    publickey).digest()
>>> r = hashlib.new('ripemd160', s        ).digest()
>>> s.encode('hex')
'ea571f53cb3a9865d3dc74735e0c16643d319c6ad81e199b9c8408cecbcec7bb'
>>> r.encode('hex')
'5238c71458e464d9ff90299abca4a1d7b9cb76ab'

```

Notice that `publickey`, `s`, and `r` are all binary byte strings (because `.digest()` is being used instead of `.hexdigest()`). The only reason to convert them to hex-encoded strings is for display purposes. You can display them directly if you like, but it's not pretty....

```
>>> publickey
'\x02!\x8a\xd6\xcd\xc62\xe7\xae}\x04G#t1\x1c\xeb\xbb\xbf\n\xb5@\xd2\xd0\x8c4\x00\xbb\x84LeB1'

```


---

### Answer by username_2 on 2019-06-13. Score: 3

In Python3:

```
#!/usr/bin/env python3

import hashlib
import codecs

publickey = codecs.decode('025914AE6A4B33A9EB1AF17FB818556F5FC849A2B9AAF176C48983BABD75A76432', 'hex')
s = hashlib.new('sha256', publickey).digest()
r = hashlib.new('ripemd160', s).digest()

print(codecs.encode(s, 'hex').decode("utf-8"))
print(codecs.encode(r, 'hex').decode("utf-8"))

```

Output:

```
c8bf4c378130fc18fcf732051e95fce4646771639097ea19b59fb5d005bf3998
cb6af4b9e44a21f9767e8989c4413c417a8b8511

```

A greeting.


---

### Answer by username_3 on 2015-04-22. Score: 0

The hex string should be transaformed to bytes first.  
Change the string to

```
b'02218AD6CDC632E7AE7D04472374311CEBBBBF0AB540D2D08C3400BB844C654231'

```

Then you should get the expected result~

Here is a relevant post
[About performing SHA-256 hashing on the public key](https://bitcoin.stackexchange.com/questions/36802/about-performing-sha-256-hashing-on-the-public-key)

> **Comments:**
> **username_4**: This is for Python 3.x FWIW, Python 2.x doesn't distinguish between byte strings and strings, but the main thing is to be sure the hex chars are run through `binascii.unhexlify` first  

---

