forked from biemster/FindMy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_keys.py
executable file
·61 lines (48 loc) · 2.27 KB
/
generate_keys.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
#!/usr/bin/env python3
import base64,argparse
import os
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--nkeys', help='number of keys to generate', type=int, default=1)
parser.add_argument('-p', '--prefix', help='prefix of the keyfiles')
parser.add_argument('-y', '--yaml', help='yaml file where to write the list of generated keys')
parser.add_argument('-v', '--verbose', help='print keys as they are generated', action="store_true")
args = parser.parse_args()
if args.yaml:
yaml = open(args.yaml + '.yaml', 'w')
yaml.write(' keys:\n')
if not os.path.exists('keys'):
os.makedirs('keys')
for i in range(args.nkeys):
while True:
private_key = ec.generate_private_key(ec.SECP224R1(), default_backend())
public_key = private_key.public_key()
private_key_bytes = private_key.private_numbers().private_value.to_bytes(28, byteorder='big')
public_key_bytes = public_key.public_numbers().x.to_bytes(28, byteorder='big')
private_key_b64 = base64.b64encode(private_key_bytes).decode("ascii")
public_key_b64 = base64.b64encode(public_key_bytes).decode("ascii")
public_key_hash = hashes.Hash(hashes.SHA256())
public_key_hash.update(public_key_bytes)
s256_b64 = base64.b64encode(public_key_hash.finalize()).decode("ascii")
if '/' in s256_b64[:7]:
pass
else:
if args.verbose:
print('%d)' % (i + 1))
print('Private key: %s' % private_key_b64)
print('Advertisement key: %s' % public_key_b64)
print('Hashed adv key: %s' % s256_b64)
if args.prefix:
fname = '%s_%s.keys' % (args.prefix, s256_b64[:7])
else:
fname = '%s.keys' % s256_b64[:7]
#mkdir keys
with open(f"keys/{fname}", 'w') as f:
f.write('Private key: %s\n' % private_key_b64)
f.write('Advertisement key: %s\n' % public_key_b64)
f.write('Hashed adv key: %s\n' % s256_b64)
if args.yaml:
yaml.write(' - "%s"\n' % public_key_b64)
break