-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
43 lines (32 loc) · 1.2 KB
/
auth.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import hmac, base64, struct, hashlib, time, sys
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
if __name__ == "__main__":
values = sys.argv
if len(values) > 1:
print(get_totp_token(values[1]))