2023-09-11 20:30:47 +00:00
|
|
|
from hashlib import sha256
|
|
|
|
import time
|
|
|
|
|
2023-09-12 14:21:31 +00:00
|
|
|
# token = hashed password + hashed salt
|
|
|
|
def generate_token(token):
|
|
|
|
time = get_timestamp()
|
|
|
|
token = sha256((token + time).encode('utf-8')).hexdigest()
|
|
|
|
|
|
|
|
return token
|
|
|
|
|
|
|
|
# password = hashed seed password
|
|
|
|
# salt = hashed salt
|
|
|
|
def get_salted_password(password, salt):
|
|
|
|
return sha256((password + salt).encode('utf-8')).hexdigest()
|
|
|
|
|
|
|
|
def validate_token(salted_password, token, n=5):
|
|
|
|
time = get_timestamp()
|
|
|
|
tokens = []
|
|
|
|
for i in range(n):
|
|
|
|
last = tokens[-1] if len(tokens) > 0 else salted_password
|
|
|
|
tokens.append(generate_token(last))
|
|
|
|
|
|
|
|
if token[:8] == tokens[-1][:8]:
|
|
|
|
return True, i
|
|
|
|
|
|
|
|
return False, -1
|
|
|
|
|
|
|
|
def get_timestamp():
|
|
|
|
return time.strftime('%d%m%Y%H%M')
|