import unittest
import devolutions_crypto
class TestPasswordHash(unittest.TestCase):
def test_hash_password_default(self):
password = b'my_secure_password'
hash_value = devolutions_crypto.hash_password(password)
self.assertTrue(devolutions_crypto.verify_password(password, hash_value))
def test_hash_password_v1_pbkdf2(self):
password = b'my_secure_password'
hash_value = devolutions_crypto.hash_password(password, version=devolutions_crypto.PasswordHashVersion.V1)
self.assertTrue(devolutions_crypto.verify_password(password, hash_value))
def test_verify_wrong_password(self):
hash_value = devolutions_crypto.hash_password(b'correct_password')
self.assertFalse(devolutions_crypto.verify_password(b'wrong_password', hash_value))
def test_hash_is_non_deterministic(self):
password = b'same_password'
hash1 = devolutions_crypto.hash_password(password)
hash2 = devolutions_crypto.hash_password(password)
self.assertNotEqual(hash1, hash2)
self.assertTrue(devolutions_crypto.verify_password(password, hash1))
self.assertTrue(devolutions_crypto.verify_password(password, hash2))
def test_hash_password_with_argon2_params(self):
password = b'my_secure_password'
params = devolutions_crypto.get_argon2_derivation_parameters(
devolutions_crypto.Argon2ParametersBuilder().build()
)
hash_value = devolutions_crypto.hash_password_with_params(password, params)
self.assertTrue(devolutions_crypto.verify_password(password, hash_value))
self.assertFalse(devolutions_crypto.verify_password(b'wrong_password', hash_value))
def test_hash_password_with_pbkdf2_params(self):
password = b'my_secure_password'
params = devolutions_crypto.get_pbkdf2_derivation_parameters(iterations=10000)
hash_value = devolutions_crypto.hash_password_with_params(password, params)
self.assertTrue(devolutions_crypto.verify_password(password, hash_value))
self.assertFalse(devolutions_crypto.verify_password(b'wrong_password', hash_value))
def test_verify_invalid_hash_raises(self):
with self.assertRaises(devolutions_crypto.DevolutionsCryptoError):
devolutions_crypto.verify_password(b'password', b'not_a_valid_hash')
def test_hash_password_with_params_invalid_params_raises(self):
with self.assertRaises(devolutions_crypto.DevolutionsCryptoError):
devolutions_crypto.hash_password_with_params(b'password', b'invalid_params')
if __name__ == "__main__":
unittest.main()