<p>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.</p>

<p><a href="https://ideone.com/DDM7vX" rel="nofollow">This is a little code snippet</a> that uses a hex2bin function to turn the hexadecimal representation (from your question) into an actual number before performing the sha256 on it.</p>

CC BY-SA 4.0
2022-08-14T08:03:58.960Z
you should inline your code snippet
hanshenrik
CC BY-SA 3.0
2016-05-22T20:48:13.747Z
2016-05-22T20:48:13.747Z
Riaan Swart
<p>Here is how to use the <a href="https://github.com/libbitcoin/libbitcoin-explorer" rel="nofollow">bitcoin-explorer</a> command line to generate an uncompressed WIF private key on a UNIX box:</p>

<blockquote>
  <p>% echo 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D | bx base58check-encode -v 128</p>
</blockquote>

<pre><code>5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ
</code></pre>

<p>The following provides feedback for going the opposite direction:</p>

<blockquote>
  <p>% echo 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ | bx base58check-decode </p>
</blockquote>

<pre><code>wrapper 
{ 
  checksum 2371582544    
  payload  0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d   
  version  128 
}
</code></pre>

<p>To create a compressed WIF private key just suffix the private hex encoded key with <strong>01</strong>:</p>

<blockquote>
  <p>% echo 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D<strong>01</strong> | bx base58check-encode -v 128</p>
</blockquote>

<p><code>KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617</code></p>

<p>Alternatively the following set of piped commands perform the same functionality:</p>

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

<p><code>0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D01</code>
<code>800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d01a62019d2</code>
<code>KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617</code></p>

<p>The following double checks the compressed WIF key encoding:</p>

<blockquote>
  <p>% echo KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617 | bx base58check-decode</p>
</blockquote>

<pre><code>wrapper 
{
  checksum 3524862118
  payload 0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d01
  version 128 
}
</code></pre>

CC BY-SA 3.0
2016-05-23T03:04:20.560Z
2016-05-24T23:34:34.233Z
2016-05-24T23:34:34.233Z
skaht
<p>The value</p>

<blockquote>
  <p>e2e4146a36e9c455cf95a4f259f162c353cd419cc3fd0e69ae36d7d1b6cd2c09</p>
</blockquote>

<p>corresponds to the SHA-256 hash of the <strong>string</strong>: </p>

<blockquote>
  <p>800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D</p>
</blockquote>

<p>In order to obtain the desired result, the key has to be interpreted as a <strong>hexadecimal value</strong>. For instance, using <a href="http://www.fileformat.info/tool/hash.htm?hex=800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D" rel="nofollow">this website</a> to compute the hash, we obtain the same result than in the wiki:</p>

<blockquote>
  <p>8147786c4d15106333bf278d71dadaf1079ef2d2440a4dde37d747ded5403592</p>
</blockquote>

CC BY-SA 3.0
2016-05-22T20:46:01.620Z
2016-05-22T20:46:01.620Z
cpsola
<p>you need to use bytes.fromhex() function.</p>

<p>here is a related example (python3)</p>

<pre><code>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')
    ripemd160.update(hashlib.sha256(bytes.fromhex(public_key)).digest())
    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))
</code></pre>

CC BY-SA 3.0
2017-07-16T05:12:47.890Z
2017-07-16T05:12:47.890Z
Mircea Stanciu
<p>There is the private_key_into_bitcoin_wif.sh script converting private key to WIF: <a href="https://gist.github.com/GregTonoski/438992249df6e4bd613f9758421ff38a" rel="nofollow noreferrer">https://gist.github.com/GregTonoski/438992249df6e4bd613f9758421ff38a</a></p>
<p>Usage example:</p>
<pre><code>bash private_key_into_bitcoin_wif.sh 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D
</code></pre>

CC BY-SA 4.0
2022-08-22T11:01:30.903Z
2022-08-22T11:01:30.903Z
Greg Tonoski
<p>simplest way</p>
<pre class="lang-py prettyprint-override"><code>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()

</code></pre>

CC BY-SA 4.0
2022-12-15T16:24:08.153Z
2022-12-15T16:24:08.153Z
fredsta98
<p>On the Bitcoin wiki it has <a href="https://en.bitcoin.it/wiki/Wallet_import_format" rel="nofollow">steps</a> to convert a private key to WIF. So I tried following these steps in attempt to get the same result.</p>

<p>So I took the private key.</p>

<pre><code>0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D
</code></pre>

<p>I put 80 on the front.</p>

<pre><code>800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D
</code></pre>

<p>And then I performed SHA-256 on it using <a href="http://www.xorbin.com/tools/sha256-hash-calculator" rel="nofollow">this</a>.</p>

<pre><code>e2e4146a36e9c455cf95a4f259f162c353cd419cc3fd0e69ae36d7d1b6cd2c09
</code></pre>

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

<pre><code>8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592
</code></pre>

<p>And that does not match my SHA-256 result.</p>

<p>Not sure what I am doing wrong.</p>

<private-key><sha256><base58><wallet-import-format>
Correct way to convert a 256-bit private key to WIF?
Nick
# Correct way to convert a 256-bit private key to WIF?

(Asked by: username_0 on 2016-05-22)

On the Bitcoin wiki it has [steps](https://en.bitcoin.it/wiki/Wallet_import_format) 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.

```
0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

```

I put 80 on the front.

```
800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

```

And then I performed SHA-256 on it using [this](http://www.xorbin.com/tools/sha256-hash-calculator).

```
e2e4146a36e9c455cf95a4f259f162c353cd419cc3fd0e69ae36d7d1b6cd2c09

```

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

```
8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592

```

And that does not match my SHA-256 result.

Not sure what I am doing wrong.

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


---

## 6 answers

### Answer by username_1 on 2016-05-22. Score: 3 | Accepted answer

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](https://ideone.com/DDM7vX) that uses a hex2bin function to turn the hexadecimal representation (from your question) into an actual number before performing the sha256 on it.

> **Comments:**
> **username_2**: you should inline your code snippet  

---

### Answer by username_3 on 2016-05-23. Score: 2

Here is how to use the [bitcoin-explorer](https://github.com/libbitcoin/libbitcoin-explorer) command line to generate an uncompressed WIF private key on a UNIX box:

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

```
5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

```

The following provides feedback for going the opposite direction:

> % echo 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ | bx base58check-decode

```
wrapper 
{ 
  checksum 2371582544    
  payload  0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d   
  version  128 
}

```

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

> % echo 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D**01** | bx base58check-encode -v 128

`KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617`

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

```
wrapper 
{
  checksum 3524862118
  payload 0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d01
  version 128 
}

```


---

### Answer by username_4 on 2016-05-22. Score: 1

The value

> e2e4146a36e9c455cf95a4f259f162c353cd419cc3fd0e69ae36d7d1b6cd2c09

corresponds to the SHA-256 hash of the **string**:

> 800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

In order to obtain the desired result, the key has to be interpreted as a **hexadecimal value**. For instance, using [this website](http://www.fileformat.info/tool/hash.htm?hex=800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D) to compute the hash, we obtain the same result than in the wiki:

> 8147786c4d15106333bf278d71dadaf1079ef2d2440a4dde37d747ded5403592


---

### Answer by username_5 on 2017-07-16. Score: 1

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')
    ripemd160.update(hashlib.sha256(bytes.fromhex(public_key)).digest())
    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))

```


---

### Answer by username_6 on 2022-08-22. Score: 0

There is the private\_key\_into\_bitcoin\_wif.sh script converting private key to WIF: <https://gist.github.com/username_6/438992249df6e4bd613f9758421ff38a>

Usage example:

```
bash private_key_into_bitcoin_wif.sh 0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

```


---

### Answer by username_7 on 2022-12-15. Score: 0

simplest way

```py
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()

```


---

