-
Notifications
You must be signed in to change notification settings - Fork 1
/
generating_private_key.py
88 lines (72 loc) · 2.79 KB
/
generating_private_key.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import secrets
import time
import random
# I refered this site
# https://www.freecodecamp.org/news/how-to-generate-your-very-own-bitcoin-private-key-7ad0f4936e6c/
def simple_method():
#Module which can produce cryptographically strong RNG
#Dont use any module like random because it is based on seed and by default the seed is the current time.
import secrets
bits = secrets.randbits(256)
print("Binary :",bits)
bits_hex = hex(bits)
print("HexaDecmal :",bits_hex)
private_key = bits_hex[2:]
print("Private Key :",private_key)
#Specialized site
#https://www.bitaddress.org
# I referred this github account
# https://github.com/Destiner/blocksmith/blob/master/blocksmith/generator.py
# The Key_Generator class is the real implementation of this site "https://www.bitaddress.org"
class Key_Generator:
def __init__(self):
self.POOL_SIZE = 256
self.KEY_BYTES = 32
self.CURVE_ORDER = int('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16)
self.pool = [0] * self.POOL_SIZE
self.pool_pointer = 0
self.prng_state = None
self.__init_pool()
def seed_input(self, str_input):
time_int = int(time.time())
self.__seed_int(time_int)
for char in str_input:
char_code = ord(char)
self.__seed_byte(char_code)
def generate_key(self):
big_int = self.__generate_big_int()
big_int = big_int % (self.CURVE_ORDER - 1) # key < curve order
big_int = big_int + 1 # key > 0
key = hex(big_int)[2:]
# Add leading zeros if the hex key is smaller than 64 chars
key = key.zfill(self.KEY_BYTES * 2)
return key
def __init_pool(self):
for i in range(self.POOL_SIZE):
random_byte = secrets.randbits(8)
self.__seed_byte(random_byte)
time_int = int(time.time())
self.__seed_int(time_int)
def __seed_int(self, n):
self.__seed_byte(n)
self.__seed_byte(n >> 8)
self.__seed_byte(n >> 16)
self.__seed_byte(n >> 24)
def __seed_byte(self, n):
self.pool[self.pool_pointer] ^= n & 255
self.pool_pointer += 1
if self.pool_pointer >= self.POOL_SIZE:
self.pool_pointer = 0
def __generate_big_int(self):
if self.prng_state is None:
seed = int.from_bytes(self.pool, byteorder='big', signed=False)
random.seed(seed)
self.prng_state = random.getstate()
random.setstate(self.prng_state)
big_int = random.getrandbits(self.KEY_BYTES * 8)
self.prng_state = random.getstate()
return big_int
if __name__=='__main__':
kg = Key_Generator()
kg.seed_input("Trutly random string. I rolled a dice and got 4")
print(kg.generate_key())