<p>Yes, it's randomness is trustworthy, so it's safe.</p>

<p>The only problem I see is that "rm" does not remove all of its traces from the hard drive. The private key may be recovered. The pedantic way to erase such secrets is to use the Gutmann method which overwrites files 35 times.</p>

<p>To derive the address you need to use something like <a href="https://bitcoin.stackexchange.com/a/67093">https://bitcoin.stackexchange.com/a/67093</a></p>

CC BY-SA 4.0
2020-05-03T17:12:16.163Z
The rm is not a problem for me i want to know if the previous lines are OK. The keys seems to be very small in fact...
Bob5421
CC BY-SA 4.0
2020-05-03T17:37:20.447Z
Yes it's correct and safe. The `rand /dev/urandom` argument specifies the randomness source which is also used by a dozen of crypto(currency) software including Bitcoin Core.
MCCCS
CC BY-SA 4.0
2020-05-03T16:14:27.617Z
2020-05-03T16:14:27.617Z
MCCCS
<p>Yes, the script generates keys safely as it uses renowned OpenSSL. Note that other factors besides the tool contribute to the safety of keys generation, e.g. air-gapped computer without viruses etc.</p>
<p>The keys are not output in Bitcoin Wallet Import Format (WIF), though. They need to be converted in order to be used in Bitcoin Wallets. There is the script for the converstion: <a href="https://gist.github.com/GregTonoski/438992249df6e4bd613f9758421ff38a" rel="nofollow noreferrer">https://gist.github.com/GregTonoski/438992249df6e4bd613f9758421ff38a</a></p>
<p>There is the Seed Tool to generate Bitcoin address: <a href="https://bitcoiner.guide/seed/" rel="nofollow noreferrer">https://bitcoiner.guide/seed/</a> (look for the &quot;Single Signature Address&quot; section).</p>

CC BY-SA 4.0
2022-08-22T11:16:56.977Z
2022-08-22T11:16:56.977Z
Greg Tonoski
<p>The format written and read by most openssl commandline utilities, although it is textual in nature, is specifically PEM and usually named accordingly, not 'text' or 'txt' which can be (and frequently is) zillions of other formats that <em>aren't</em> PEM and wouldn't be usable here.</p>
<p><strong>Some other things you can do with openssl that might help:</strong></p>
<pre><code># instead of head/tail/etc you can use asn1parse -strparse to extract the private and public values
# but as raw/binary not hex:
$ openssl asn1parse &lt;privpem -strparse 5 -out privraw &gt;&amp;/dev/null; od -Ax -tx1 privraw
000000 23 34 02 73 64 bb 35 79 f0 f5 25 76 f5 f9 5f df
000010 ff aa 94 d3 24 21 da c9 e3 2d 33 95 8b 8d 4f 1d
000020
$ openssl asn1parse &lt;privpem -strparse 50 -out pubraw &gt;&amp;/dev/null; od -Ax -tx1 pubraw
000000 04 f8 04 f9 ea 79 7e 9b 07 89 e7 37 f2 76 c4 86
000010 16 17 f9 70 4e c4 67 ce 6e 64 ca 3a 5a bf 4d cb
000020 98 e1 11 0b 7a ec 40 f3 ea 32 d4 e9 6c 16 6e 14
000030 e3 77 15 1a 2e c2 81 a6 ed de f5 5c ed cb 10 26
000040 09
000041
# (the &gt;&amp;/dev/null discards error messages because the contents of these fields are not ASN.1)
# (or you can omit it and just ignore the messages)
</code></pre>
<pre><code># openssl ec -text directly displays (all) the fields of the PEM file, 
# with the two you want in hex:
$ openssl ec &lt;privpem -noout -text
read EC key
Private-Key: (256 bit)
priv:
    23:34:02:73:64:bb:35:79:f0:f5:25:76:f5:f9:5f:
    df:ff:aa:94:d3:24:21:da:c9:e3:2d:33:95:8b:8d:
    4f:1d
pub:
    04:f8:04:f9:ea:79:7e:9b:07:89:e7:37:f2:76:c4:
    86:16:17:f9:70:4e:c4:67:ce:6e:64:ca:3a:5a:bf:
    4d:cb:98:e1:11:0b:7a:ec:40:f3:ea:32:d4:e9:6c:
    16:6e:14:e3:77:15:1a:2e:c2:81:a6:ed:de:f5:5c:
    ed:cb:10:26:09
ASN1 OID: secp256k1
# you can then postprocess this data as desired, 
# excluding the first line which is on stderr and not easily pipable, e.g.:
$ openssl ec &lt;privpem -noout -text 2&gt;/dev/null | sed -n 3,5p | tr -d ' :\n'; echo
2334027364bb3579f0f52576f5f95fdfffaa94d32421dac9e32d33958b8d4f1d
$ openssl ec &lt;privpem -noout -text 2&gt;/dev/null | sed -n 7,11p | tr -d ' :\n'; echo
04f804f9ea797e9b0789e737f276c4861617f9704ec467ce6e64ca3a5abf4dcb98e1110b7aec40f3ea32d4e96c166e14e377151a2ec281a6eddef55cedcb102609
</code></pre>
<pre><code># given the _binary_ publickey, as from asn1parse above, 
# openssl can compute the address in _hex_ or binary 
# but not base58check (or bech32) as often wanted:
$ openssl sha256 &lt;pubraw -binary | openssl ripemd160
(stdin)= 149a326797fdfd3790fba8f82571fd5f569341cc
# for binary output add -binary to the second subcommand,
# and redirect to a file &gt;outfile because it isn't displayable

# by default openssl creates an EC key in uncompressed form, but you can specify, 
# or subsequently convert to, compressed; the asn1parse output depends on its input.
# The ec -text output can be converted in the same operation, e.g:
$ openssl ec &lt;privpem -conv_form compressed -noout -text
read EC key
Private-Key: (256 bit)
priv:
    23:34:02:73:64:bb:35:79:f0:f5:25:76:f5:f9:5f:
    df:ff:aa:94:d3:24:21:da:c9:e3:2d:33:95:8b:8d:
    4f:1d
pub:
    03:f8:04:f9:ea:79:7e:9b:07:89:e7:37:f2:76:c4:
    86:16:17:f9:70:4e:c4:67:ce:6e:64:ca:3a:5a:bf:
    4d:cb:98
ASN1 OID: secp256k1
# shows the compressed pubkey regardless of whether the file was compressed
# and now you would select lines 7-9 instead of 7-11.
</code></pre>

CC BY-SA 4.0
2020-10-02T12:08:51.063Z
2020-10-02T12:08:51.063Z
dave_thompson_085
<p>I want to understand how bitcoin works and I do not trust tools or bitcoin platforms.</p>
<p>I have read this on Internet. Can you confirm me this lines are generating a bitcoin public/private key safely ?</p>
<pre><code>openssl ecparam -genkey -name secp256k1 -rand /dev/urandom -out tmp_private.txt
openssl ec -in tmp_private.txt -outform DER \
    | tail -c +8 | head -c 32 | xxd -p -c 32 &gt; private.txt
openssl ec -in tmp_private.txt -pubout -outform DER \
    | tail -c 65 | xxd -p -c 65 &gt; public.txt
rm tmp_private.txt
</code></pre>
<p>I know it is not a good thing to store private key in a basic clear text file, I just want to know if the way generating the key is good.</p>
<p>Is there an openssl command which can generate a bitcoin address from public.txt file?</p>

CC BY-SA 4.0
2020-05-06T19:05:10.180Z
Linux has `shred` which can be substituted for `rm`
Peleion
CC BY-SA 4.0
2020-10-02T13:41:18.263Z
Shred is notoriously  unreliable for this stuff - modern filesystems, especially those with copy on write, or even SSDs and some HDDs will often use different blocks to store the shredded data - in many cases, the original keys will still be recoverable if you use shred.
Raghav Sood
<address><private-key><openssl>
Generate public/private key for bitcoin with openssl
Bob5421
# Generate public/private key for bitcoin with openssl

(Asked by: username_0 on 2020-05-03)

I want to understand how bitcoin works and I do not trust tools or bitcoin platforms.

I have read this on Internet. Can you confirm me this lines are generating a bitcoin public/private key safely ?

```
openssl ecparam -genkey -name secp256k1 -rand /dev/urandom -out tmp_private.txt
openssl ec -in tmp_private.txt -outform DER \
    | tail -c +8 | head -c 32 | xxd -p -c 32 > private.txt
openssl ec -in tmp_private.txt -pubout -outform DER \
    | tail -c 65 | xxd -p -c 65 > public.txt
rm tmp_private.txt

```

I know it is not a good thing to store private key in a basic clear text file, I just want to know if the way generating the key is good.

Is there an openssl command which can generate a bitcoin address from public.txt file?

**Tags:** address, private-key, openssl

> **Comments:**
> **username_1**: Linux has `shred` which can be substituted for `rm`  
> **username_2**: Shred is notoriously  unreliable for this stuff - modern filesystems, especially those with copy on write, or even SSDs and some HDDs will often use different blocks to store the shredded data - in many cases, the original keys will still be recoverable if you use shred.  

---

## 3 answers

### Answer by username_3 on 2020-05-03. Score: 1

Yes, it's randomness is trustworthy, so it's safe.

The only problem I see is that "rm" does not remove all of its traces from the hard drive. The private key may be recovered. The pedantic way to erase such secrets is to use the Gutmann method which overwrites files 35 times.

To derive the address you need to use something like <https://bitcoin.stackexchange.com/a/67093>

> **Comments:**
> **username_0**: The rm is not a problem for me i want to know if the previous lines are OK. The keys seems to be very small in fact...  
> **username_3**: Yes it's correct and safe. The `rand /dev/urandom` argument specifies the randomness source which is also used by a dozen of crypto(currency) software including Bitcoin Core.  

---

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

Yes, the script generates keys safely as it uses renowned OpenSSL. Note that other factors besides the tool contribute to the safety of keys generation, e.g. air-gapped computer without viruses etc.

The keys are not output in Bitcoin Wallet Import Format (WIF), though. They need to be converted in order to be used in Bitcoin Wallets. There is the script for the converstion: <https://gist.github.com/username_4/438992249df6e4bd613f9758421ff38a>

There is the Seed Tool to generate Bitcoin address: <https://bitcoiner.guide/seed/> (look for the "Single Signature Address" section).


---

### Answer by username_5 on 2020-10-02. Score: 0

The format written and read by most openssl commandline utilities, although it is textual in nature, is specifically PEM and usually named accordingly, not 'text' or 'txt' which can be (and frequently is) zillions of other formats that *aren't* PEM and wouldn't be usable here.

**Some other things you can do with openssl that might help:**

```
# instead of head/tail/etc you can use asn1parse -strparse to extract the private and public values
# but as raw/binary not hex:
$ openssl asn1parse <privpem -strparse 5 -out privraw >&/dev/null; od -Ax -tx1 privraw
000000 23 34 02 73 64 bb 35 79 f0 f5 25 76 f5 f9 5f df
000010 ff aa 94 d3 24 21 da c9 e3 2d 33 95 8b 8d 4f 1d
000020
$ openssl asn1parse <privpem -strparse 50 -out pubraw >&/dev/null; od -Ax -tx1 pubraw
000000 04 f8 04 f9 ea 79 7e 9b 07 89 e7 37 f2 76 c4 86
000010 16 17 f9 70 4e c4 67 ce 6e 64 ca 3a 5a bf 4d cb
000020 98 e1 11 0b 7a ec 40 f3 ea 32 d4 e9 6c 16 6e 14
000030 e3 77 15 1a 2e c2 81 a6 ed de f5 5c ed cb 10 26
000040 09
000041
# (the >&/dev/null discards error messages because the contents of these fields are not ASN.1)
# (or you can omit it and just ignore the messages)

```

```
# openssl ec -text directly displays (all) the fields of the PEM file, 
# with the two you want in hex:
$ openssl ec <privpem -noout -text
read EC key
Private-Key: (256 bit)
priv:
    23:34:02:73:64:bb:35:79:f0:f5:25:76:f5:f9:5f:
    df:ff:aa:94:d3:24:21:da:c9:e3:2d:33:95:8b:8d:
    4f:1d
pub:
    04:f8:04:f9:ea:79:7e:9b:07:89:e7:37:f2:76:c4:
    86:16:17:f9:70:4e:c4:67:ce:6e:64:ca:3a:5a:bf:
    4d:cb:98:e1:11:0b:7a:ec:40:f3:ea:32:d4:e9:6c:
    16:6e:14:e3:77:15:1a:2e:c2:81:a6:ed:de:f5:5c:
    ed:cb:10:26:09
ASN1 OID: secp256k1
# you can then postprocess this data as desired, 
# excluding the first line which is on stderr and not easily pipable, e.g.:
$ openssl ec <privpem -noout -text 2>/dev/null | sed -n 3,5p | tr -d ' :\n'; echo
2334027364bb3579f0f52576f5f95fdfffaa94d32421dac9e32d33958b8d4f1d
$ openssl ec <privpem -noout -text 2>/dev/null | sed -n 7,11p | tr -d ' :\n'; echo
04f804f9ea797e9b0789e737f276c4861617f9704ec467ce6e64ca3a5abf4dcb98e1110b7aec40f3ea32d4e96c166e14e377151a2ec281a6eddef55cedcb102609

```

```
# given the _binary_ publickey, as from asn1parse above, 
# openssl can compute the address in _hex_ or binary 
# but not base58check (or bech32) as often wanted:
$ openssl sha256 <pubraw -binary | openssl ripemd160
(stdin)= 149a326797fdfd3790fba8f82571fd5f569341cc
# for binary output add -binary to the second subcommand,
# and redirect to a file >outfile because it isn't displayable

# by default openssl creates an EC key in uncompressed form, but you can specify, 
# or subsequently convert to, compressed; the asn1parse output depends on its input.
# The ec -text output can be converted in the same operation, e.g:
$ openssl ec <privpem -conv_form compressed -noout -text
read EC key
Private-Key: (256 bit)
priv:
    23:34:02:73:64:bb:35:79:f0:f5:25:76:f5:f9:5f:
    df:ff:aa:94:d3:24:21:da:c9:e3:2d:33:95:8b:8d:
    4f:1d
pub:
    03:f8:04:f9:ea:79:7e:9b:07:89:e7:37:f2:76:c4:
    86:16:17:f9:70:4e:c4:67:ce:6e:64:ca:3a:5a:bf:
    4d:cb:98
ASN1 OID: secp256k1
# shows the compressed pubkey regardless of whether the file was compressed
# and now you would select lines 7-9 instead of 7-11.

```


---

