private key is a random 256 bit number.
It is used as the source of a
Generate a Private Key
All you need to generate a private key is a reliable source of randomness.
An easy source of randomness on a Linux computer is
/dev/urandom, which provides random bits of data from your computer. All you need to do is read from it:
# generate 256 bits of random data urandom = File.open("/dev/urandom") # urandom is a "file" bytes = urandom.read(32) # read 32 bytes from it (256 bits) privatekey = bytes.unpack("H*") # the data is binary, so unpack it to hexadecimal # print the private key puts privatekey
A private key can be almost any 256-bit number.
When you create a public key, your private key is put through a special mathematical function, and this function can only handle numbers up to just below 256 bits. The maximum value is:
max = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1 max = 115792089237316195423570985008687907853269984665640564039457584007908834671663
So when you generate a 256 bit number, you will want to check that it's below this maximum value.
# print a warning if private key is too big if privatekey.to_i(16) > 115792089237316195423570985008687907853269984665640564039457584007908834671663 puts "Private key is too big. Try again." end
A hexadecimal private key is 64 characters in length:
If you're generating private keys for your own personal use, this is all you really need.
Wallet Import Format
However, you can convert your private key to a WIF Private Key, which basically makes it easier to copy.