<p>Using <a href="https://github.com/mcdallas/cryptotools" rel="nofollow noreferrer">this</a> Python library (I am the author)</p>
<pre><code>&gt;&gt;&gt; from cryptotools import PrivateKey
&gt;&gt;&gt; i = 1
&gt;&gt;&gt; prv = PrivateKey(i.to_bytes(32, ‘big’))
&gt;&gt;&gt; prv.wif()
‘5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf‘
</code></pre>

CC BY-SA 4.0
2021-08-10T13:13:22.170Z
Thanks , Exactly what i needed
Guest34122123
CC BY-SA 4.0
2021-08-10T14:47:45.010Z
This code gets compressed key, Is there anyway to get Uncompressed from same way.
Thanks
Guest34122123
CC BY-SA 4.0
2021-08-10T14:59:08.710Z
The code above is uncompressed. For compressed use `prv.wif(compressed=True)`
Mike D
CC BY-SA 4.0
2021-08-10T19:17:30.260Z
It gives me different result for compressed key 
for example [1]: https://i.sstatic.net/Ydbhn.png
This key is 769
But the script give me this key 
[2]: https://i.sstatic.net/48GFY.png

What am i doing wrong 
What change do i have to do to get KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU74sHUHy8S , from i = 769
Guest34122123
CC BY-SA 4.0
2021-08-10T21:23:34.127Z
I think the brainwallet site is padding to 33 bytes in the last screenshot so replace 32 with 33
Mike D
CC BY-SA 4.0
2021-08-10T12:15:33.213Z
2021-08-10T12:15:33.213Z
Mike D
<blockquote>
<p><strong>If i do this other way on this same site, It gives me different result</strong></p>
</blockquote>
<p>Try entering <code>0000000000000000000000000000000000000000000000000000000000000001</code> and selecting <code>Hex</code> at the top and <code>Base58check</code> at the bottom.</p>
<p><a href="https://i.sstatic.net/CnEkp.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/CnEkp.jpg" alt="brainwallet webpage" /></a></p>
<hr />
<blockquote>
<p><strong>I want a formula to convert This 1 into this private key (5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf) in python</strong></p>
</blockquote>
<p><a href="https://www.google.com/search?q=python+base58check" rel="nofollow noreferrer">https://www.google.com/search?q=python+base58check</a> leads to <a href="https://pypi.org/project/base58check/" rel="nofollow noreferrer">https://pypi.org/project/base58check/</a> and many others.</p>
<p>Note that these libraries require that your value 1 be expressed as a byte array, not as a single decimal integer.</p>
<hr />
<p>Remember that what you are seeing is not <em>conversion of a single number into a private key</em> but simply the same number presented in two different representations. That is, two different encodings. Note also that encoding is not encryption.</p>
<ul>
<li><p>1 is the same number as 0x01 even though the latter is encoded as hexadecimal.</p>
</li>
<li><p>1 is the same number as 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf even though the latter is encoded as Base58check.</p>
</li>
</ul>

CC BY-SA 4.0
2021-08-10T12:20:13.110Z
Any idea how can i convert 1 into the byte array and then convert that bye array into the privatekey 5HpHag...... I want to do all of these in python
Guest34122123
CC BY-SA 4.0
2021-08-10T09:43:12.027Z
2021-08-10T09:55:44.687Z
2021-08-10T09:55:44.687Z
RedGrittyBrick
<p>You can also do it &quot;from scratch&quot; with just hashlib, binascii and base58 modules:</p>
<pre><code>import hashlib, binascii, base58

pkeyhash = &quot;0000000000000000000000000000000000000000000000000000000000000001&quot;

pkeyvbytes = &quot;80&quot;+pkeyhash

firsthash = hashlib.sha256(binascii.unhexlify(pkeyvbytes)).hexdigest()

secondhash = hashlib.sha256(binascii.unhexlify(firsthash)).hexdigest()

createkey = pkeyvbytes+secondhash[:8]

WIF = base58.b58encode(binascii.unhexlify(createkey))

print(WIF)

&gt;&gt;&gt; 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf
</code></pre>

CC BY-SA 4.0
2021-08-11T05:08:49.697Z
2021-08-11T05:08:49.697Z
m1xolyd1an
<p><a href="https://i.sstatic.net/cBGdZ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/cBGdZ.png" alt="enter image description here" /></a></p>
<p><a href="https://brainwalletx.github.io/#converter" rel="nofollow noreferrer">https://brainwalletx.github.io/#converter</a>,
5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf</p>
<p>This private key  is generated from 1</p>
<p>If i do this other way on this same site, It gives me different result</p>
<p><a href="https://i.sstatic.net/foDqu.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/foDqu.png" alt="enter image description here" /></a>
I want a formula to convert This 1 into this private key(5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf) in python
Anyone knows formula or python code to do this ?</p>

CC BY-SA 4.0
2021-08-10T12:42:43.043Z
You can go to the chapter 4 but doing the whole thing from the start would be best https://github.com/jimmysong/programmingbitcoin
Saxtheowl
<python>
How to convert a single number into Private key in python
Guest34122123
# How to convert a single number into Private key in python

(Asked by: username_0 on 2021-08-10)

[![enter image description here](https://i.sstatic.net/cBGdZ.png)](https://i.sstatic.net/cBGdZ.png)

<https://brainwalletx.github.io/#converter>,
5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf

This private key is generated from 1

If i do this other way on this same site, It gives me different result

[![enter image description here](https://i.sstatic.net/foDqu.png)](https://i.sstatic.net/foDqu.png)
I want a formula to convert This 1 into this private key(5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf) in python
Anyone knows formula or python code to do this ?

**Tags:** python

> **Comments:**
> **username_1**: You can go to the chapter 4 but doing the whole thing from the start would be best https://github.com/jimmysong/programmingbitcoin  

---

## 3 answers

### Answer by username_2 on 2021-08-10. Score: 1 | Accepted answer

Using this Python library (I am the author)
>>> from cryptotools import PrivateKey
>>> i = 1
>>> prv = PrivateKey(i.to_bytes(32, ‘big’))
>>> prv.wif()
‘5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf‘

> **Comments:**
> **username_0**: Thanks , Exactly what i needed  
> **username_0**: This code gets compressed key, Is there anyway to get Uncompressed from same way. Thanks  
> **username_2**: The code above is uncompressed. For compressed use `prv.wif(compressed=True)`  
> **username_0**: It gives me different result for compressed key  for example [1]: https://i.sstatic.net/Ydbhn.png This key is 769 But the script give me this key  [2]: https://i.sstatic.net/48GFY.png  What am i doing wrong  What change do i have to do to get KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU74sHUHy8S , from i = 769  
> **username_2**: I think the brainwallet site is padding to 33 bytes in the last screenshot so replace 32 with 33  

---

### Answer by username_3 on 2021-08-10. Score: 1

> **If i do this other way on this same site, It gives me different result**

Try entering `0000000000000000000000000000000000000000000000000000000000000001` and selecting `Hex` at the top and `Base58check` at the bottom.

[![brainwallet webpage](https://i.sstatic.net/CnEkp.jpg)](https://i.sstatic.net/CnEkp.jpg)

---

> **I want a formula to convert This 1 into this private key (5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf) in python**

<https://www.google.com/search?q=python+base58check> leads to <https://pypi.org/project/base58check/> and many others.

Note that these libraries require that your value 1 be expressed as a byte array, not as a single decimal integer.

---

Remember that what you are seeing is not *conversion of a single number into a private key* but simply the same number presented in two different representations. That is, two different encodings. Note also that encoding is not encryption.

* 1 is the same number as 0x01 even though the latter is encoded as hexadecimal.
* 1 is the same number as 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf even though the latter is encoded as Base58check.

> **Comments:**
> **username_0**: Any idea how can i convert 1 into the byte array and then convert that bye array into the privatekey 5HpHag...... I want to do all of these in python  

---

### Answer by username_4 on 2021-08-11. Score: 0

You can also do it "from scratch" with just hashlib, binascii and base58 modules:
import hashlib, binascii, base58

pkeyhash = "0000000000000000000000000000000000000000000000000000000000000001"

pkeyvbytes = "80"+pkeyhash

firsthash = hashlib.sha256(binascii.unhexlify(pkeyvbytes)).hexdigest()

secondhash = hashlib.sha256(binascii.unhexlify(firsthash)).hexdigest()

createkey = pkeyvbytes+secondhash[:8]

WIF = base58.b58encode(binascii.unhexlify(createkey))

print(WIF)

>>> 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf


---

