def test_salt(self): """ Tests that password hashes are salted :return: None """ password = generate_random(30) hash_one = generate_hash(password) hash_two = generate_hash(password) self.assertNotEqual(hash_one, hash_two) self.assertTrue(verify_password(password, hash_one)) self.assertTrue(verify_password(password, hash_two))
def test_verifying_with_invalid_hash(self): """ Tests that attempting to verify a password with an incorrectly formatted hash will return False :return: None """ self.assertFalse(verify_password("A", "A"))
def verify_confirmation(self, confirmation_key: str) -> bool: """ Verifies a confirmation key against the confirmation hash :param confirmation_key: The key to check :return: True if the key matches, False otherwise """ return verify_password(confirmation_key, self.confirmation_hash)
def verify_password(self, password: str) -> bool: """ Verifies a password against the password hash :param password: The password to check :return: True if the password matches, False otherwise """ return verify_password(password, self.password_hash)
def test_hashing_strings(self): """ Tests that password hashing and verifying works with string as well :return: None """ password = str(generate_random(30)) _hash = generate_hash(password) self.assertTrue(verify_password(password, _hash))
def test_hashing(self): """ Tests that passwords can be hashed successfully :return: None """ password_one = generate_random(30) password_two = generate_random(20) hash_one = generate_hash(password_one) hash_two = generate_hash(password_two) self.assertEqual(len(hash_one), len(hash_two)) self.assertNotEqual(hash_one, hash_two) self.assertTrue(verify_password(password_one, hash_one)) self.assertTrue(verify_password(password_two, hash_two)) self.assertFalse(verify_password(password_one, hash_two)) self.assertFalse(verify_password(password_two, hash_one))
def verify_key(self, key: str) -> bool: """ Checks if a given key is valid :param key: The key to check :return: True if the key is valid, False otherwise """ try: _id, api_key = key.split(":", 1) if int(_id) != self.id: return False else: return verify_password(api_key, self.key_hash) except ValueError: return False