On the Bitcoin wiki it has steps to convert a private key to WIF. So I tried following these steps in attempt to get the same result.

So I took the private key.


I put 80 on the front.


And then I performed SHA-256 on it using this.


And that is where I run into a problem. On the wiki page it says the result of SHA-256 should be...


And that does not match my SHA-256 result.

Not sure what I am doing wrong.


6 Answers 6


You're performing a SHA256 on an ASCII string, not on the actual number. That ASCII string is actually the hexadecimal representation of the actual number.

This is a little code snippet that uses a hex2bin function to turn the hexadecimal representation (from your question) into an actual number before performing the sha256 on it.

  • you should inline your code snippet
    – hanshenrik
    Commented Aug 14, 2022 at 8:03

Here is how to use the bitcoin-explorer command line to generate an uncompressed WIF private key on a UNIX box:

% echo 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D | bx base58check-encode -v 128


The following provides feedback for going the opposite direction:

% echo 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ | bx base58check-decode

  checksum 2371582544    
  payload  0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d   
  version  128 

To create a compressed WIF private key just suffix the private hex encoded key with 01:

% echo 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D01 | bx base58check-encode -v 128


Alternatively the following set of piped commands perform the same functionality:

% echo 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D | sed 's/$/01/' | bx wrap-encode -v 128 | bx base58-encode

0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D01 800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d01a62019d2 KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617

The following double checks the compressed WIF key encoding:

% echo KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617 | bx base58check-decode

  checksum 3524862118
  payload 0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d01
  version 128 

The value


corresponds to the SHA-256 hash of the string:


In order to obtain the desired result, the key has to be interpreted as a hexadecimal value. For instance, using this website to compute the hash, we obtain the same result than in the wiki:



you need to use bytes.fromhex() function.

here is a related example (python3)

import hashlib
import ecdsa
import base58

def generate_private_and_public_keys(secret):
    #hash digest
    digest = hashlib.sha256(secret.encode()).hexdigest()

    #signing and verification keys
    signing_key = ecdsa.SigningKey.from_string(bytes.fromhex(digest), curve=ecdsa.SECP256k1)

    verifying_key = signing_key.verifying_key
    public_key = '04' + verifying_key.to_string().hex()

    return signing_key.to_string().hex(), public_key;

def get_wif_public_key(public_key):
    ripemd160 = hashlib.new('ripemd160')
    mm = '00' + ripemd160.digest().hex()

    checksum = hashlib.sha256((hashlib.sha256(bytes.fromhex(mm)).digest())).digest()[0:4]

    binary_addr = mm + checksum.hex()
    return base58.b58encode(bytes.fromhex(binary_addr))

private_key, public_key = generate_private_and_public_keys('erfjer;ljfecfv kfk  gf 74764 dlr;f;cmiofmsr')
print ('Private_key: {}'.format(private_key))
print ('Public key: {}'.format(public_key))

wif = get_wif_public_key(public_key)
print ('Bitcoin address: {}'.format(wif))

There is the private_key_into_bitcoin_wif.sh script converting private key to WIF: https://gist.github.com/GregTonoski/438992249df6e4bd613f9758421ff38a

Usage example:

bash private_key_into_bitcoin_wif.sh 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

simplest way

from bit import Key

# Import hex/-256-bit format:
privKey = Key.from_hex('L1VotKmtZRLZSnSPhLhQxfts2aqBMru2APTs4Yuc8TYJ4jNhQoGB')

# Export to wif
privKey = privKey.to_wif()

# Export int format:

privKey = Key.to_int()

# Export to bytes
privKey = Key.to_bytes()

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.