-
Notifications
You must be signed in to change notification settings - Fork 0
/
kamitoken.py
72 lines (58 loc) · 1.96 KB
/
kamitoken.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
from cryptography.fernet import Fernet
import hashlib , base64
from datetime import datetime
import json
class kamitoken:
def __init__(self, key : str = "kami" , time : int = 300 ) -> None:
self.key = self.getkey(key)
self.time = time *60 # x phút
def getkey (self , passwd : str ):
# get key from passwd
has_passwd = hashlib.sha256(passwd.encode()).digest()
key_enc = base64.urlsafe_b64encode(has_passwd[:32])
return key_enc
def encode (self, User : str , passwd : str ):
data = {
"user": User,
"passwd":passwd,
"time": int(datetime.now().timestamp())
}
enc = Fernet(self.key)
kami = str(data)
data_enc = enc.encrypt(kami.encode())
return data_enc.decode("utf-8")
def get_time(self,time : int ):
now = int(datetime.now().timestamp())
kq = now - time
print(kq)
if kq>= self.time:
return False
else:
return True
def decode( self, data : str):
try:
key_en = Fernet(self.key)
data_decode = key_en.decrypt(data).decode()
data = data_decode.replace("'", '"') # data_decode = {'user': 'kami@123.com', 'time': 1726709984}
data = json.loads(data)
now = int(datetime.now().timestamp())
if self.get_time(int(data['time'])):
return True , data
else:
return False, 0
except Exception as e :
print("lỗi tại ", e )
return False , 0
# async def endcode(user : str ):
# async def decode(data : str):
token = kamitoken(key="qsuangprovip", time=5)
key = token.encode("kami@123.com", "12345")
import time
time.sleep(6)
check , data = token.decode(key)
if check:
print(data)
print(key)
print(token.key)
else:
print("token đã hiết Hạn rồi em ")