forked from al177/twostep
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathconfiguration.py
executable file
·61 lines (53 loc) · 1.59 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
#!/usr/bin/env python
# genKeyLine code by Hordur Heidarsson
# rest Oliver Matthews
import base64
import sys
secrets = []
labels = []
lengths = []
time_zone = "+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) + " },"
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#define _CONFIGURATION_H_\n" )
f.write( "#define NUM_SECRETS %i\n" % len(labels) )
f.write( "#define DEFAULT_TIME_ZONE %s\n" % time_zone )
f.write( "char otplabels[NUM_SECRETS][10] = {\n " )
for label in labels:
f.write( "\"%s\"," % label )
f.write( "\n};\n" )
f.write( "unsigned char otpkeys[NUM_SECRETS][%s] = {\n " % max(lengths) )
for secret in secrets:
f.write( "%s\n" % secret )
f.write( "};\n" )
f.write ("int otpsizes[NUM_SECRETS] = { ")
for length in lengths:
f.write( "%s," % length )
f.write( "};\n#endif\n" )
f.close()