-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2FA.py
executable file
·48 lines (35 loc) · 837 Bytes
/
2FA.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
#!/usr/bin/env python
import hmac
import base64
import hashlib
import datetime
import time
#totp
interval=30 #seconds
#otp
digest=hashlib.sha1
digits=6 #number of integers supported?
secret='123456789abcdefg'
#totp
now=datetime.datetime.now()
i=time.mktime(now.timetuple())
timecode=int(i/interval)
#otp
base64_secret = base64.b32decode(secret,casefold=True)
res = []
while timecode != 0:
res.append(chr(timecode & 0xFF))
timecode = timecode >> 8
bytestring=''.join(reversed(res)).rjust(8,'\0') #padding=8
hmac_hash = hmac.new(
base64_secret,
bytestring,
digest
).digest()
offset=ord(hmac_hash[19]) & 0xf
code = ((ord(hmac_hash[offset]) & 0x7f) << 24 |
(ord(hmac_hash[offset + 1]) & 0xff) << 16 |
(ord(hmac_hash[offset + 2]) & 0xff) << 8 |
(ord(hmac_hash[offset + 3]) & 0xff))
code = code % 10 ** digits
print code