Exemple #1
0
def get_users_priv_cubes():
    user = generate_user()
    cube = generate_cube()
    info = generate_info()

    r = remote(*ADDR)
    register(r, user, cube, info)

    users_list = list_users(r)
    priv_cubes = {}
    base_cube_inversed = Cube().move("U R' U' F'")

    for server_user in users_list:
        if server_user == user:
            continue
        server_user_pub_cube = get_users_pub_cube(r, server_user)
        priv_cubes[server_user] = [
            Cube(server_user_pub_cube).multiply(base_cube_inversed.pow(i))
            for i in range(7)
        ]

    _exit(r)
    r.close()

    return priv_cubes
Exemple #2
0
class AuthManager:
    def __init__(self, db):
        self.db = db
        self.base_cube = Cube().move("F U R U'")

    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

    def process(self):
        print(MENU)
        try:
            _input = int(input("> ").strip())
        except Exception:
            return

        if _input == 0:
            print("Bye bye!")
            return
        elif _input == 1:
            user = self.register()
        elif _input == 2:
            user = self.login()
        else:
            return

        if user is None:
            return

        return user