def get_users_pub_cube(r, user): r.sendlineafter(b'> ', b'3') r.sendlineafter(b"> ", user) r.recvuntil(b"User's public cube: ") pub_cube = r.recvline(False).decode() if pub_cube: return Cube.from_str(pub_cube)
def get_user_data(self, login): db = self._get_db() data = db.hgetall(f'{CUBES_PREFIX}{login}') pub_cube = Cube.from_str(data[b'pub_cube'].decode()) priv_key = int(data[b'priv_key'].decode()) info = data[b'info'].decode() if data: return pub_cube, priv_key, info
def login(r, user: str, cube: Cube): r.sendlineafter(b'> ', b'2') r.sendlineafter(b'> ', user) r.sendlineafter(b'> ', cube.as_str()) r.recvuntil(b'Your public cube is ') pub_cube = r.recvline(False).decode() try: return Cube.from_str(pub_cube) except Exception: return None
def login(self): print("Your name:") login = input("> ") if not self.db.check_user(login): print('There are no user with your login! Register first!') return pub_cube, priv_key, info = self.db.get_user_data(login) print(f'Give me your cube:') try: priv_cube = Cube.from_str(input("> ").strip()) except Exception: print("This is not a cube!") return if priv_cube.multiply(self.base_cube.pow(priv_key)) != pub_cube: print('Wrong cube!') return print(f'Hello!\nYour public cube is {pub_cube.as_str()}') return login, pub_cube, info
def register(self): print("Your name:") login = input("> ") if self.db.check_user(login): print('There exist user with your login!') return print('Give me your cube:') try: priv_cube = Cube.from_str(input("> ").strip()) except Exception: print("This is not a cube!") return priv_key = randint(1, ORDER) pub_cube = priv_cube.multiply(self.base_cube.pow(priv_key)) print('Anything about yourself:') info = input("> ") self.db.set_user_data(login, pub_cube, priv_key, info) print(f'Hello!\nYour public cube is {pub_cube.as_str()}') return login, pub_cube, info