Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: isolutioncorp on April 15, 2021, 01:09:21 AM



Title: what is grep command in Linux to find hex private key in my .txt file? i saved
Post by: isolutioncorp on April 15, 2021, 01:09:21 AM
what is grep command in Linux to find hex private key in my .txt file? i saved it somewhere on a backup drive, im not talking about WIF privatekey

please help!


Title: Re: what is grep command in Linux to find hex private key in my .txt file? i saved
Post by: odolvlobo on April 15, 2021, 01:30:41 AM
what is grep command in Linux to find hex private key in my .txt file? i saved it somewhere on a backup drive, im not talking about WIF privatekey

Not an expert but I think this will work:

grep -E '[a-fA-F0-9]{64,64}[^a-fA-F0-9]' <file>



Title: Re: what is grep command in Linux to find hex private key in my .txt file? i saved
Post by: isolutioncorp on April 15, 2021, 01:36:25 AM
thanks a lot, will give it a try and let you know, i tried googling this first only came up for WIF not hex private keys


Title: Re: what is grep command in Linux to find hex private key in my .txt file? i saved
Post by: NotATether on April 15, 2021, 06:38:20 AM
grep -E -o '[1234567890abcdefABCDEF]{64}' also does the job and using the -o option, only prints the part of the line that actually matches the private key. This avoids printing extremely large lines or perhaps an entire binary file that happens to have a matching string.

You should probably redirect the output to a file because most terminals' output buffers are fairly small, only holding he last 1000 or so lines of output.


Title: Re: what is grep command in Linux to find hex private key in my .txt file? i saved
Post by: 10_sjdovn_10 on April 19, 2021, 08:39:47 AM
grep -E -o '[1234567890abcdefABCDEF]{64}' also does the job and using the -o option, only prints the part of the line that actually matches the private key. This avoids printing extremely large lines or perhaps an entire binary file that happens to have a matching string.

You should probably redirect the output to a file because most terminals' output buffers are fairly small, only holding he last 1000 or so lines of output.

@NotATether
I don't understand, every private key is Hex type but they showed with A-Z 0-9 characters !?
How about finding a public or private key in wallet files (specially those are not encrypted) dose this technic works on them?


Title: Re: what is grep command in Linux to find hex private key in my .txt file? i saved
Post by: DannyHamilton on April 19, 2021, 01:15:09 PM
I don't understand, every private key is Hex type but they showed with A-Z 0-9 characters

If you are seeing characters from G - Z, then it is NOT hex.  Hex generally only uses numeric digits (0-9) and the first 6 characters of the alphabet in either capital or lowercase form  (A - F and a - f).

Additionally, you only want to use grep if the key is stored as UTF-8 encoded string of characters representing the key in a human-readable format.

How about finding a public or private key in wallet files (specially those are not encrypted) dose this technic works on them?

That depends on the file and which wallet software it came from.  Many wallets store the key in binary format rather than an encoded string.  In that case, using grep will be more complicated and messy and probably isn't the best tool for the job.


Title: Re: what is grep command in Linux to find hex private key in my .txt file? i saved
Post by: 10_sjdovn_10 on April 19, 2021, 03:24:06 PM
I don't understand, every private key is Hex type but they showed with A-Z 0-9 characters

If you are seeing characters from G - Z, then it is NOT hex.  Hex generally only uses numeric digits (0-9) and the first 6 characters of the alphabet in either capital or lowercase form  (A - F and a - f).

Additionally, you only want to use grep if the key is stored as UTF-8 encoded string of characters representing the key in a human-readable format.

How about finding a public or private key in wallet files (specially those are not encrypted) dose this technic works on them?

That depends on the file and which wallet software it came from.  Many wallets store the key in binary format rather than an encoded string.  In that case, using grep will be more complicated and messy and probably isn't the best tool for the job.

Right private keys are Hex but why there is  K, w, y and .... in private key ? Aha, they turn 128bit binary to hex then find it's related Characters ! and show set of characters !!


Title: Re: what is grep command in Linux to find hex private key in my .txt file? i saved
Post by: NotATether on April 19, 2021, 04:42:33 PM
Right private keys are Hex but why there is  K, w, y and .... in private key ? Aha, they turn 128bit binary to hex then find it's related Characters ! and show set of characters !!

If you see a private key with those kind of characters then that is not a hexadecimal formatted key, it is a WIF private key encoded in Base58 (or if it begins with "bc1", bech32).

This regex should find Base58 keys:

Code:
[123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ]{51,52}


Additionally, the following regex will find bech32 private keys addresses:

Code:
bc1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{6,87}


Title: Re: what is grep command in Linux to find hex private key in my .txt file? i saved
Post by: NotATether on April 20, 2021, 10:23:45 AM
Additionally, the following regex will find bech32 private keys:

Code:
bc1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{6,87}

I notice {6,87} which AFAIK means string with range 6-87 character. Any reason you chose that range because AFAIK Bech32 address isn't that short.

BIP173 said that addresses can be up to 90 characters long, including the human readable part and separator. The data part after bc1 is at least 6 characters long, and 90 when the length of bc1 is taken out is 87.

Granted, I haven't seen Bech32 addresses that small in practice, because the hash160 has a fixed length.


Title: Re: what is grep command in Linux to find hex private key in my .txt file? i saved
Post by: NotATether on April 20, 2021, 11:17:50 AM
On a side note, which wallet uses Bech32 as format of the private key?

Cripes, I must've posted the regex for bech32 address instead  :-[ there is no such thing as a Bech32 WIF private key.