<p>This is working for me:</p>

<pre><code>import os
import ecdsa
import binascii
import hashlib

private_key = os.urandom(32)
PK_EN = binascii.hexlify(private_key)
print("Your Key" + "  " + str(PK_EN))
PK_CON = binascii.unhexlify(PK_EN)
sk = ecdsa.SigningKey.from_string(PK_CON, curve=ecdsa.SECP256k1)
vk = sk.verifying_key
msg = binascii.hexlify(b'Hello')
sign_msg = sk.sign(msg)
assert vk.verify(sign_msg, msg)

public_key = b'04' + binascii.hexlify(vk.to_string())
print(public_key)

ripemd160 = hashlib.new('ripemd160')

ripemd160.update(hashlib.sha256(binascii.unhexlify(public_key)).digest())
</code></pre>

<p>I've changed the way you concatenated the public key with the prefix, and the call to the <code>ripmd160.update</code>, since it was expecting a string, not a Hash object.</p>

CC BY-SA 3.0
2017-09-28T14:46:12.460Z
2017-09-28T15:43:56.017Z
2017-09-28T15:43:56.017Z
sr_gi
<p><a href="https://gist.github.com/vivosmith/f0e692d9aedbc39ec8d3eb4651bc107c" rel="nofollow noreferrer">https://gist.github.com/vivosmith/f0e692d9aedbc39ec8d3eb4651bc107c</a></p>

<p>I was trying to implement <a href="https://www.youtube.com/watch?v=tX-XokHf_nI" rel="nofollow noreferrer">this video</a> in Python 3</p>

<p>But the issue is that, it was implemented in Python 2</p>

<p>So I tried to implement this with binascii instead of encode/decode. However I keep getting a binascii.Error: Odd-length string at line 22.</p>

<p>How to I correct this? Or if this is completely broken, then is there any other libraries that work better for python 3?</p>

<python>
binascii.Error: Odd-length string for Python Implementation of Bitcoin
user1192890
# binascii.Error: Odd-length string for Python Implementation of Bitcoin

(Asked by: username_0 on 2017-09-25)

<https://gist.github.com/vivosmith/f0e692d9aedbc39ec8d3eb4651bc107c>

I was trying to implement [this video](https://www.youtube.com/watch?v=tX-XokHf_nI) in Python 3

But the issue is that, it was implemented in Python 2

So I tried to implement this with binascii instead of encode/decode. However I keep getting a binascii.Error: Odd-length string at line 22.

How to I correct this? Or if this is completely broken, then is there any other libraries that work better for python 3?

**Tags:** python


---

## 1 answers

### Answer by username_1 on 2017-09-28. Score: 1

This is working for me:
import os
import ecdsa
import binascii
import hashlib

private_key = os.urandom(32)
PK_EN = binascii.hexlify(private_key)
print("Your Key" + "  " + str(PK_EN))
PK_CON = binascii.unhexlify(PK_EN)
sk = ecdsa.SigningKey.from_string(PK_CON, curve=ecdsa.SECP256k1)
vk = sk.verifying_key
msg = binascii.hexlify(b'Hello')
sign_msg = sk.sign(msg)
assert vk.verify(sign_msg, msg)

public_key = b'04' + binascii.hexlify(vk.to_string())
print(public_key)

ripemd160 = hashlib.new('ripemd160')

ripemd160.update(hashlib.sha256(binascii.unhexlify(public_key)).digest())

I've changed the way you concatenated the public key with the prefix, and the call to the ripmd160.update, since it was expecting a string, not a Hash object.


---

