ut_scrypt.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.ut_scrypt: password hashing unit test for the MMGen suite
  4. """
  5. from mmgen.util import msg, msg_r
  6. from ..include.common import cfg, qmsg, vmsg, omsg_r, silence, end_silence
  7. class unit_test:
  8. def run_test(self, name, ut):
  9. import time
  10. msg_r('Testing password hashing...')
  11. qmsg('')
  12. from mmgen.crypto import Crypto
  13. crypto = Crypto(cfg)
  14. salt = bytes.fromhex('f00f' * 16)
  15. presets = {
  16. '1': '64898d01ffcea1252c17411e7bd6c12d00191ee8ddcbf95420e5de8f37a7a1cb',
  17. '2': '625d0c59fcbcf85eca51b4337d3a4952e554c7c95fe32f4199ad0e9e9370e279',
  18. '3': 'b30a5cf0e606515be4a983cc4a1a4c21e04806f19aaa0ad354a353c83aecd3ee',
  19. '4': 'a3a1b99393734510b68adf2c2cfdc627a2bc3281913d8ea6fbb677d39781a9fa',
  20. '5': '0c7e1a672738cee49cf0ff6f3208190ca418e741835fd6995ce9558cc19f3f04',
  21. '6': '91f9d1c9baf3948433dab58dcc912d96035392c1db21ede96d2f369e025ab06d',
  22. '7': 'fcb2cd05268de43b0d2d45f78a56e5d446b0bd2d3b57bdbc77cc17a42942f1bd'
  23. }
  24. pws = (
  25. ('', 'cc8d99ce7365d8a9d2422d71ce330e130b2cade46a8cc0459a3f83e1a6ac3d30'),
  26. ('foo', 'f0e2cce1d9980edf2373a2070ad3560c2506faf9bc50704a1bc5cdb3c7f63f3b'),
  27. ('φυβαρ', '64898d01ffcea1252c17411e7bd6c12d00191ee8ddcbf95420e5de8f37a7a1cb')
  28. )
  29. def test_passwords():
  30. for pw_base, res in pws:
  31. for pw in (pw_base, pw_base.encode()):
  32. pw_disp = "'"+pw+"'" if isinstance(pw, str) else "b'"+pw.decode()+"'"
  33. if cfg.quiet:
  34. omsg_r('.')
  35. else:
  36. msg_r(f'\n password {pw_disp:9} ')
  37. ret = crypto.scrypt_hash_passphrase(pw, salt, '1').hex()
  38. assert ret == res, ret
  39. def test_presets(do_presets):
  40. for hp in do_presets:
  41. hp = str(hp)
  42. res = presets[hp]
  43. pw = 'φυβαρ'
  44. if cfg.quiet:
  45. omsg_r('.')
  46. else:
  47. msg_r(f'\n {hp!r:3}: {crypto.hash_presets[hp]!r:12} ')
  48. st = time.time()
  49. ret = crypto.scrypt_hash_passphrase(pw, salt, hp).hex()
  50. t = time.time() - st
  51. vmsg('' if cfg.test_suite_deterministic else f' {t:0.4f} secs')
  52. assert ret == res, ret
  53. if cfg.quiet:
  54. silence()
  55. cfg.force_standalone_scrypt_module = False
  56. vmsg('Passwords (auto module selection):')
  57. test_passwords()
  58. vmsg('Hash presets (auto module selection):')
  59. test_presets((1, 2, 3, 4) if cfg.fast else (1, 2, 3, 4, 5, 6, 7))
  60. cfg.force_standalone_scrypt_module = True
  61. vmsg('Passwords (force standalone scrypt module):')
  62. test_passwords()
  63. vmsg('Hash presets (force standalone scrypt module):')
  64. test_presets((1, 2, 3))
  65. if cfg.quiet:
  66. end_silence()
  67. msg('OK')
  68. return True