-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathotp.py
More file actions
31 lines (22 loc) · 676 Bytes
/
otp.py
File metadata and controls
31 lines (22 loc) · 676 Bytes
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
import time
import random
import hashlib
import os
def encode_with_otp(pw: str) -> str:
CHARS = os.getenv("CHARS")
seed = seed_from_timestamp(str(timestamp_in_minutes()))
random.seed(seed)
otp = ""
length = len(CHARS)
for s in range(256):
otp += CHARS[int(random.random() * length)]
return shuffle(otp, pw)
def seed_from_timestamp(t: str) -> int:
h = hashlib.sha256((t + os.getenv("TIMESTAMP_SALT")).encode())
return int.from_bytes(h.digest(), 'big')
def shuffle(otp: str, pw: str) -> str:
o = list(otp+pw)
random.shuffle(o)
return "".join(o)
def timestamp_in_minutes() -> int:
return int(time.time() / 60)