Exemplo n.º 1
0
 def _encrypt_for_client(self):
     if self.action not in ['authenticate', 'register']:
         key = ServerSession.get_client_session_key(self.sender)
         self.response = Encryptor.symmetrical_encrypt(key=key,
                                                       message=json.dumps(
                                                           self.response))
         self.response = self.response.decode()
Exemplo n.º 2
0
 def _search(self):
     if Users.user_exists(self.content['username']):
         self.response['key'] = Encryptor.get_public_key_as_string(
             Users.get_public_key(self.content['username']))
         self.response['username'] = self.content['username']
     else:
         self.status = ResponseCreator.status_codes['USER_DO_NOT_EXIST']
Exemplo n.º 3
0
 def serialize(cls,
               location: str = 'data/',
               file_name: str = 'users_to_public_keys.txt'):
     res = {}
     for k, v in cls.get_instance().users_to_public_keys.items():
         res[k] = Encryptor.get_public_key_as_string(v)
     with open(location + file_name, 'wb') as f:
         f.write(json.dumps(res).encode())
Exemplo n.º 4
0
def test_encryption_time():
    pub_k, pr_k = Encryptor.generate_keys()

    start = timer()
    for i in range(500):
        encrypted = Encryptor.asymmetric_encrypt_message(
            b'Some stupid message', pub_k)
    end = timer()
    print("500 encryptions time: " + str(end - start))

    encrypted = Encryptor.asymmetric_encrypt_message(b'Some stupid message',
                                                     pub_k)
    start = timer()
    for i in range(500):
        descripted = Encryptor.asymmetric_decrypt_message(encrypted, pr_k)
    end = timer()
    print("500 decryptions time: " + str(end - start))
Exemplo n.º 5
0
 def _authenticate(self):
     username = self.content['username']
     if not Users.user_exists(username):
         self.status = ResponseCreator.status_codes['USER_DO_NOT_EXIST']
         return
     user = Users.get_user(username)
     password = Encryptor.asymmetric_decrypt_message(
         key=ServerSession.get_private_key(),
         message=self.content['password'])
     if user.check_password(password):
         key = Encryptor.generate_symmetric_key()
         ServerSession.add_client_session_key(user.get_username(), key)
         pub_k = UsersToPublicKeys.get_public_key(username)
         print(username, ' session key ', key)
         encrypted_key = Encryptor.asymmetric_encrypt_message(key=pub_k,
                                                              message=key)
         self.response = {'key': encrypted_key}
     else:
         self.status = ResponseCreator.status_codes['WRONG_PASSWORD']
Exemplo n.º 6
0
 def _delete_account(self):
     username = self.content['username']
     if not Users.user_exists(username):
         self.status = ResponseCreator.status_codes['USER_DO_NOT_EXIST']
         return
     user = Users.get_user(username)
     password = Encryptor.asymmetric_decrypt_message(
         key=ServerSession.get_private_key(),
         message=self.content['password'])
     if user.check_password(password):
         Users.delete_user_account(username)
Exemplo n.º 7
0
 def _register(self):
     username = self.content['username']
     if Users.user_exists(username):
         self.status = ResponseCreator.status_codes['LOGIN_NOT_AVAILABLE']
         return
     password = self.content['password']
     public_key = self.content['public_key']
     email = self.content['email']
     usr = User(username=username,
                password=Encryptor.hash_message(password.encode()).decode(),
                email=email)
     Users.add_user(usr)
     UsersToPublicKeys.add_user_public_key(username, public_key)
Exemplo n.º 8
0
 def deserialize(cls,
                 location: str = 'data/',
                 file_name: str = 'users_to_public_keys.txt'):
     try:
         res = {}
         with open(location + file_name, 'rb') as f:
             file_content = f.read()
             if file_content != b'':
                 res = json.loads(file_content)
                 for k, v in res.items():
                     cls.get_instance().users_to_public_keys[
                         k] = Encryptor.load_public_key_from_string(v)
     except FileNotFoundError:
         print('No such file')
Exemplo n.º 9
0
 def __repr__(self):
     res = {}
     for k, v in self.users_to_public_keys.items():
         res[k] = Encryptor.get_public_key_as_string(v)
     return json.dumps(res, indent=3)
Exemplo n.º 10
0
 def check_password(self, password: str) -> bool:
     password = Encryptor.hash_message(password.encode())
     password = password.decode()
     return password == self.password
Exemplo n.º 11
0
 def add_user_public_key(cls, username: str, public_key):
     if isinstance(public_key, str):
         public_key = Encryptor.load_public_key_from_string(public_key)
     cls.get_instance().users_to_public_keys[username] = public_key
Exemplo n.º 12
0
def generate_new_keys():
    pub_key, pr_key = Encryptor.generate_keys()
    Encryptor.save_public_key(pub_key)
    Encryptor.save_private_key(pr_key)
Exemplo n.º 13
0
 def __init__(self):
     self.server_private_key = Encryptor.load_private_key()
     self.server_public_key = Encryptor.load_public_key()
     self.clients_to_session_keys: Dict[str, bytes] = {}
Exemplo n.º 14
0
 def _decrypt_from_client(self):
     if self.action not in ['authenticate', 'register']:
         key = ServerSession.get_client_session_key(self.sender)
         self.content = Encryptor.symmetrical_decrypt(key=key,
                                                      message=self.content)
         self.content = json.loads(self.content)