<p>By comparing all variables with <a href="https://gobittest.appspot.com/PrivateKey" rel="nofollow noreferrer">this tool</a>, I've found that the mistake is in this line:</p>

<pre><code>print b58(extend.decode('hex') + sha2[:8])
</code></pre>

<p>Remember that <code>sha2</code> is a byte array and not a hex string. You can replace the <code>8</code> with a <code>4</code> to make your code work.</p>

<p>You can also simplify the code even further to:</p>

<pre><code>def b58(hex):
    return base58.b58encode_check(hex)

def sha256(hex):
    return hashlib.sha256(hex).digest()

def main():
    k = sha256('private_key')
    extend = '80'.decode('hex') + k
    print b58(extend)
</code></pre>

CC BY-SA 4.0
2019-10-28T17:59:38.983Z
2019-10-28T17:59:38.983Z
MCCCS
<p>For today's Python 3 I found working this code:</p>
<pre><code>#!/usr/bin/env python3
import base58
import hashlib
import sys

def b58(hex):
    return base58.b58encode_check(hex)

def sha256(hex):
    return hashlib.sha256(hex).digest()

def main():
    k = sha256(str(sys.argv[1]).encode('utf-8'))
    extend = '80' + k.hex()
    print(b58(bytes.fromhex(extend)).decode('utf-8'))

if __name__ == '__main__':
    main()
</code></pre>

CC BY-SA 4.0
2020-06-19T17:52:39.443Z
2020-06-19T17:52:39.443Z
pbies
<p>i try to generate WIF private key with python.</p>

<pre><code>import base58
import hashlib

def b58(hex):
    return base58.b58encode(hex)

def sha256(hex):
    return hashlib.sha256(hex).digest()

def main():
    k = sha256('private_key')
    extend = '80' + k.encode('hex')
    sha1 = sha256(extend.decode('hex'))
    sha2 = sha256(sha1)
    print b58(extend.decode('hex') + sha2[:8])

if __name__ == '__main__':
    main()
</code></pre>

<p>the result's prefix is not '5' but 'V'</p>

<p>i try to just base58encode 0x80 and the result is 3D ???</p>

<p>where is mistake in this code?</p>

<wallet><private-key><wallet-import-format>
When generate WIF private key , base58check('80'.decode('hex')) not prefix as '5'
LeePanda
# When generate WIF private key , base58check('80'.decode('hex')) not prefix as '5'

(Asked by: username_0 on 2019-10-28)

i try to generate WIF private key with python.
import base58
import hashlib

def b58(hex):
    return base58.b58encode(hex)

def sha256(hex):
    return hashlib.sha256(hex).digest()

def main():
    k = sha256('private_key')
    extend = '80' + k.encode('hex')
    sha1 = sha256(extend.decode('hex'))
    sha2 = sha256(sha1)
    print b58(extend.decode('hex') + sha2[:8])

if __name__ == '__main__':
    main()

the result's prefix is not '5' but 'V'
i try to just base58encode 0x80 and the result is 3D ???
where is mistake in this code?

**Tags:** wallet, private-key, wallet-import-format


---

## 2 answers

### Answer by username_1 on 2019-10-28. Score: 1 | Accepted answer

By comparing all variables with this tool, I've found that the mistake is in this line:
print b58(extend.decode('hex') + sha2[:8])

Remember that sha2 is a byte array and not a hex string. You can replace the 8 with a 4 to make your code work.
You can also simplify the code even further to:
def b58(hex):
    return base58.b58encode_check(hex)

def sha256(hex):
    return hashlib.sha256(hex).digest()

def main():
    k = sha256('private_key')
    extend = '80'.decode('hex') + k
    print b58(extend)


---

### Answer by username_2 on 2020-06-19. Score: 0

For today's Python 3 I found working this code:
#!/usr/bin/env python3
import base58
import hashlib
import sys

def b58(hex):
    return base58.b58encode_check(hex)

def sha256(hex):
    return hashlib.sha256(hex).digest()

def main():
    k = sha256(str(sys.argv[1]).encode('utf-8'))
    extend = '80' + k.hex()
    print(b58(bytes.fromhex(extend)).decode('utf-8'))

if __name__ == '__main__':
    main()


---

