-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfiguration.py
executable file
·65 lines (56 loc) · 1.64 KB
/
configuration.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
#!/usr/bin/env python
import base64
import sys
secrets = []
labels = []
lengths = []
time_zone = "0.0"
def genKeyLine(code):
secret_key = code.replace(' ','').upper()
if len(secret_key) <= 32:
key_b32 = secret_key+'='*(32%len(secret_key))
key = base64.b32decode(key_b32)
else:
key_b64 = secret_key+'='*(64%len(secret_key))
key = base64.b32decode(key_b64)
key_bytes = map(ord,key)
lengths.append(len(key_bytes))
key_hex = ["0x%02X" % x for x in key_bytes]
return '{ ' + ', '.join(key_hex) + ' },'
def generate():
try:
f = open('configuration.txt', 'r')
except:
print 'Unable to open configuration.txt. Cheack README.md for configuration details.'
sys.exit(1)
for line in f:
line = line.strip()
if line.startswith('#') or not ':' in line: continue
key,value = line.split(':')
if (key.lower() == 'tz'):
time_zone = value
else:
labels.append(key)
secrets.append(genKeyLine(value))
f.close()
f = open('src/configuration.h', 'w')
f.write("#ifndef _CONFIGURATION_H_\n")
f.write("#define _CONFIGURATION_H_\n\n")
f.write("#define DEFAULT_TIME_ZONE %s\n" % time_zone)
f.write("#define NUM_SECRETS %i\n\n" % len(labels))
f.write("char otp_labels[NUM_SECRETS][17] = {\n\t")
for label in labels:
f.write("\"%s\"," % label)
f.write("\n};\n\n")
f.write("unsigned char otp_keys[NUM_SECRETS][%s] = {\n" % max(lengths))
for secret in secrets:
f.write("\t%s\n" % secret)
f.write("};\n\n")
f.write("int otp_sizes[NUM_SECRETS] = {")
for length in lengths:
f.write("%s," % length)
f.write("};\n\n#endif\n")
f.close()
# generate configuration.h if this file is run as the main script
if __name__ == '__main__':
generate()