3
2
Would this Python code generate a valid ECDSA private key?
import random
def r(a, b):
sys_ran = random.SystemRandom()
return sys_ran.randint(a, b)
def create_private_key():
hex_chars = '0123456789ABCDEF'
ran_hex = '';
for i in range(64):
ran_hex += hex_chars[r(0, 15)]
max_hex = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'
if int(ran_hex, 16) <= int(max_hex, 16):
return ran_hex
else:
return create_private_key()
private_key = create_private_key()
print('Private key: ' + private_key)
As far as I can tell from the Bitcoin wiki, an ECDSA private key is any 64 character hexadecimal (or the corresponding decimal) number between 0x1 and 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141. It doesn't mention anything about any special algorithms required for the private key to be valid.
3
Welcome to Bitcoin.SE! Whilst this may theoretically answer the question, it would be preferable to include some explanation of your code too so that people can understand how it works and what it does
– MeshCollider – 2018-01-16T09:09:14.683