-
Notifications
You must be signed in to change notification settings - Fork 1
/
totp.py
96 lines (72 loc) · 2.39 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# import httplib2
# import hmac
# import hashlib
# import time
# import sys
# import struct
# import json
# userid = "[email protected]"
# secret_suffix = "HENNGECHALLENGE003"
# shared_secret = userid+secret_suffix
# timestep = 30
# T0 = 0
# def HOTP(K, C, digits=10):
# K_bytes = str.encode(K)
# C_bytes = struct.pack(">Q", C)
# hmac_sha512 = hmac.new(key=K_bytes, msg=C_bytes,
# digestmod=hashlib.sha512).hexdigest()
# return Truncate(hmac_sha512)[-digits:]
# def Truncate(hmac_sha512):
# offset = int(hmac_sha512[-1], 16)
# binary = int(hmac_sha512[(offset * 2):((offset*2)+8)], 16) & 0x7FFFFFFF
# return str(binary)
# def TOTP(K, digits=10, timeref=0, timestep=30):
# C = int(time.time()-timeref)//timestep
# return HOTP(K, C, digits=digits)
# passwd = TOTP(shared_secret, 10, T0, timestep).zfill(10)
# print(passwd)
import requests
import hmac
import hashlib
import time
import sys
import struct
import json
from requests.auth import HTTPBasicAuth
root = "https://api.challenge.hennge.com/challenges/003"
content_type = "application/json"
userid = "[email protected]"
secret_suffix = "HENNGECHALLENGE003"
shared_secret = userid+secret_suffix
timestep = 30
T0 = 0
def HOTP(K, C, digits=10):
"""HTOP:
K is the shared key
C is the counter value
digits control the response length
"""
K_bytes = str.encode(K)
C_bytes = struct.pack(">Q", C)
hmac_sha512 = hmac.new(key=K_bytes, msg=C_bytes,
digestmod=hashlib.sha512).hexdigest()
return Truncate(hmac_sha512)[-digits:]
def Truncate(hmac_sha512):
"""truncate sha512 value"""
offset = int(hmac_sha512[-1], 16)
binary = int(hmac_sha512[(offset * 2):((offset*2)+8)], 16) & 0x7FFFFFFF
return str(binary)
def TOTP(K, digits=10, timeref=0, timestep=30):
"""TOTP, time-based variant of HOTP
digits control the response length
the C in HOTP is replaced by ( (currentTime - timeref) / timestep )
"""
C = int(time.time() - timeref) // timestep
return HOTP(K, C, digits=digits)
data = {"github_url": "https://gist.github.com/manisaiprasad/bcde82c08f0958a1a49d0c3aac6d9093",
"contact_email": "[email protected]"}
passwd = TOTP(shared_secret, 10, T0, timestep).zfill(10)
resp = requests.post(root, auth=HTTPBasicAuth(
userid, passwd), data=json.dumps(data))
print(resp.json())
print(passwd)