2023-06-13 21:12:46 -04:00
|
|
|
from mail4one.pwhash import gen_pwhash, parse_hash, check_pass, SALT_LEN, KEY_LEN
|
2023-06-06 00:00:36 -04:00
|
|
|
import unittest
|
2023-05-15 22:41:21 -04:00
|
|
|
|
2023-06-06 00:00:36 -04:00
|
|
|
|
|
|
|
class TestPWHash(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_expected_usage(self):
|
|
|
|
password = "Blah Blah ABCD"
|
|
|
|
pwhash = gen_pwhash(password)
|
2023-05-15 22:41:21 -04:00
|
|
|
pwinfo = parse_hash(pwhash)
|
2023-06-06 00:00:36 -04:00
|
|
|
self.assertEqual(len(pwinfo.salt), SALT_LEN)
|
2023-06-07 16:24:06 -04:00
|
|
|
self.assertEqual(len(pwinfo.scrypt_hash), KEY_LEN)
|
2023-06-06 00:00:36 -04:00
|
|
|
self.assertTrue(check_pass(password, pwinfo),
|
|
|
|
"check pass with correct password")
|
|
|
|
self.assertFalse(check_pass("foobar", pwinfo),
|
|
|
|
"check pass with wrong password")
|
2023-05-15 22:41:21 -04:00
|
|
|
|
2023-06-06 00:00:36 -04:00
|
|
|
def test_hardcoded_hash(self):
|
2023-06-10 23:28:45 -04:00
|
|
|
test_hash = "".join(c for c in """
|
2023-06-07 16:24:06 -04:00
|
|
|
AFTY5EVN7AX47ZL7UMH3BETYWFBTAV3XHR73CEFAJBPN2NIHPWD
|
|
|
|
ZHV2UQSMSPHSQQ2A2BFQBNC77VL7F2UKATQNJZGYLCSU6C43UQD
|
|
|
|
AQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF
|
2023-06-10 23:28:45 -04:00
|
|
|
""" if not c.isspace())
|
2023-06-06 00:00:36 -04:00
|
|
|
pwinfo = parse_hash(test_hash)
|
|
|
|
self.assertTrue(check_pass("helloworld", pwinfo),
|
|
|
|
"check pass with correct password")
|
|
|
|
self.assertFalse(check_pass("foobar", pwinfo),
|
|
|
|
"check pass with wrong password")
|
2023-05-15 22:41:21 -04:00
|
|
|
|
2023-06-06 00:00:36 -04:00
|
|
|
def test_invalid_hash(self):
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
parse_hash("sdlfkjdsklfjdsk")
|
2023-05-15 22:41:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-06-06 00:00:36 -04:00
|
|
|
unittest.main()
|