diff options
| author | hc <hc@email.ch> | 2024-11-20 12:51:33 +0800 |
|---|---|---|
| committer | hc <hc@email.ch> | 2024-11-20 12:51:33 +0800 |
| commit | 853b82126baa1e8e408a10f91053c52626ffad29 (patch) | |
| tree | 2fc1de9695810681ba654aab3c2a4867aacc1ac7 /tfa.py | |
| parent | b1f88b682624e85b4b743343dfaaeed113b69413 (diff) | |
working
Diffstat (limited to 'tfa.py')
| -rw-r--r-- | tfa.py | 71 |
1 files changed, 71 insertions, 0 deletions
| @@ -0,0 +1,71 @@ | |||
| 1 | import time | ||
| 2 | from typing import Dict, List | ||
| 3 | import random | ||
| 4 | import string | ||
| 5 | |||
| 6 | class customstore: | ||
| 7 | def __init__(self, ttl=300, maxsize=200): | ||
| 8 | self.store: Dict[str, List] = {} # key -> [expiry_time, verified_status] | ||
| 9 | self.ttl = ttl | ||
| 10 | #self.maxsize = maxsize, now dont need this | ||
| 11 | |||
| 12 | def create(self): | ||
| 13 | self.clean() | ||
| 14 | keylength = 4 | ||
| 15 | key_added = False | ||
| 16 | while not key_added: | ||
| 17 | current_time = int(time.time()) | ||
| 18 | key = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(keylength)) | ||
| 19 | if key not in self.store: | ||
| 20 | self.store[key] = [current_time + self.ttl, False] | ||
| 21 | key_added = True | ||
| 22 | return key | ||
| 23 | |||
| 24 | def authenticate(self, key): | ||
| 25 | current_time = int(time.time()) | ||
| 26 | if key in self.store and current_time <= self.store[key][0]: | ||
| 27 | self.store[key][1] = True | ||
| 28 | return True | ||
| 29 | else: | ||
| 30 | return False | ||
| 31 | |||
| 32 | def check(self, key): | ||
| 33 | current_time = int(time.time()) | ||
| 34 | if key in self.store and current_time <= self.store[key][0] and self.store[key][1] == True: | ||
| 35 | return True | ||
| 36 | else: | ||
| 37 | return False | ||
| 38 | |||
| 39 | def clean(self): | ||
| 40 | current_time = int(time.time()) | ||
| 41 | expired_keys = [k for k, [expiry_time, _] in self.store.items() if current_time > expiry_time] | ||
| 42 | for key in expired_keys: | ||
| 43 | self.store.pop(key, None) | ||
| 44 | return | ||
| 45 | |||
| 46 | if __name__ == "__main__": | ||
| 47 | s1 = customstore(ttl=7) | ||
| 48 | |||
| 49 | # Create and verify first key | ||
| 50 | k = s1.create() | ||
| 51 | print("Store state:", s1.store) | ||
| 52 | print("Created key:", k) | ||
| 53 | print("First verification:", "yeppy" if s1.authenticate(k) else "nopey") | ||
| 54 | print("Second verification:", "yeppy" if s1.check(k) else "nopey") | ||
| 55 | # Wait and try again | ||
| 56 | time.sleep(5) | ||
| 57 | k2 = s1.create() | ||
| 58 | print("\nStore state after 5 seconds:", s1.store) | ||
| 59 | print("First key:", k) | ||
| 60 | print("First key verification:", "yeppy" if s1.check(k) else "nopey") | ||
| 61 | |||
| 62 | # Wait and try again | ||
| 63 | time.sleep(5) | ||
| 64 | print("\nStore state after 5 seconds:", s1.store) | ||
| 65 | print("First key:", k) | ||
| 66 | print("First key verification:", "yeppy" if s1.check(k) else "nopey") | ||
| 67 | k = s1.create() | ||
| 68 | print("Store state:", s1.store) | ||
| 69 | print("Created key:", k) | ||
| 70 | print("First verification:", "yeppy" if s1.authenticate(k) else "nopey") | ||
| 71 | print("Second verification:", "yeppy" if s1.check(k) else "nopey") | ||
