-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTOTP.py
65 lines (56 loc) · 1.76 KB
/
TOTP.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
import hmac, base64, struct, hashlib, time
import pickle
secrets_file = "secrets.pkl"
def get_hotp_token(secret, intervals_no):
"""This is where the magic happens."""
key = base64.b32decode(normalize(secret), True) # True is to fold lower into uppercase
msg = struct.pack(">Q", intervals_no)
h = bytearray(hmac.new(key, msg, hashlib.sha1).digest())
o = h[19] & 15
h = str((struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000)
return prefix0(h)
def get_totp_token(secret):
"""The TOTP token is just a HOTP token seeded with every 30 seconds."""
return get_hotp_token(secret, intervals_no=int(time.time())//30)
def normalize(key):
"""Normalizes secret by removing spaces and padding with = to a multiple of 8"""
k2 = key.strip().replace(' ','')
# k2 = k2.upper() # skipped b/c b32decode has a foldcase argument
if len(k2)%8 != 0:
k2 += '='*(8-len(k2)%8)
return k2
def prefix0(h):
"""Prefixes code with leading zeros if missing."""
if len(h) < 6:
h = '0'*(6-len(h)) + h
return h
def ACCOUNT_ADD(name: str, secret: str) -> None:
try:
with open(secrets_file, "rb") as f:
loaded_dict = pickle.load(f)
except:
with open(secrets_file, "xb") as f:
loaded_dict = {}
pickle.dump(loaded_dict, f)
loaded_dict[name] = secret
with open(secrets_file, "wb") as f:
pickle.dump(loaded_dict, f)
def ACCOUNT_REMOVE(name: str) -> None:
with open(secrets_file, "rb") as f:
loaded_dict = pickle.load(f)
try:
del loaded_dict[name]
except:
pass
with open(secrets_file, "wb") as f:
pickle.dump(loaded_dict, f)
def SHOW_ALL_CODES() -> dict | None:
try:
with open(secrets_file, "rb") as f:
loaded_dict = pickle.load(f)
return loaded_dict
except:
return None
if __name__ == "__main__":
#ACCOUNT_ADD("John", "6HLV Z46X TNV2 NXGT")
SHOW_ALL_CODES()