-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotp.py
38 lines (27 loc) · 1.22 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
#!/usr/bin/python3
srv = { # Secret key Server name
1: {'k':'AAAAAAAAAAAAA', 'n':'My Proxmox cluster (192.168.0.*)'},
2: {'k':'BBBBBBBBBBBBB', 'n':'My GitHub (username)'},
3: {'k':'CCCCCCCCCCCCC', 'n':'Hosting, SSH'}
}
# Micro-TOTP Generator by Amin v. 2021-09-29 (v4)
# https://aminux.wordpress.com/
import base64
import hmac
import struct
import time
# imported from https://github.com/susam/mintotp/blob/master/mintotp.py
# avoid pip imports / external dependencies
def hotp(key, counter, digits=6, digest='sha1'):
key = base64.b32decode(key.upper() + '=' * ((8 - len(key)) % 8))
counter = struct.pack('>Q', counter)
mac = hmac.new(key, counter, digest).digest()
offset = mac[-1] & 0x0f
binary = struct.unpack('>L', mac[offset:offset+4])[0] & 0x7fffffff
return str(binary)[-digits:].rjust(digits, '0')
def totp(key, time_step=30, digits=6, digest='sha1'):
return hotp(key, int(time.time() / time_step), digits, digest)
remain_seconds = (30 - int(time.strftime("%S")) % 30);
print (" ----- {0:02d} ------------------------------------------------------------------".format(remain_seconds) );
for s in srv:
print (' ', totp(srv[s]['k']), '|', srv[s]['n'])