ut_scrypt.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_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 = { '1': '64898d01ffcea1252c17411e7bd6c12d00191ee8ddcbf95420e5de8f37a7a1cb',
  16. '2': '625d0c59fcbcf85eca51b4337d3a4952e554c7c95fe32f4199ad0e9e9370e279',
  17. '3': 'b30a5cf0e606515be4a983cc4a1a4c21e04806f19aaa0ad354a353c83aecd3ee',
  18. '4': 'a3a1b99393734510b68adf2c2cfdc627a2bc3281913d8ea6fbb677d39781a9fa',
  19. '5': '0c7e1a672738cee49cf0ff6f3208190ca418e741835fd6995ce9558cc19f3f04',
  20. '6': '91f9d1c9baf3948433dab58dcc912d96035392c1db21ede96d2f369e025ab06d',
  21. '7': 'fcb2cd05268de43b0d2d45f78a56e5d446b0bd2d3b57bdbc77cc17a42942f1bd' }
  22. pws = ( ('', 'cc8d99ce7365d8a9d2422d71ce330e130b2cade46a8cc0459a3f83e1a6ac3d30'),
  23. ('foo', 'f0e2cce1d9980edf2373a2070ad3560c2506faf9bc50704a1bc5cdb3c7f63f3b'),
  24. ('φυβαρ', '64898d01ffcea1252c17411e7bd6c12d00191ee8ddcbf95420e5de8f37a7a1cb') )
  25. def test_passwords():
  26. for pw_base,res in pws:
  27. for pw in (pw_base,pw_base.encode()):
  28. pw_disp = "'"+pw+"'" if isinstance(pw,str) else "b'"+pw.decode()+"'"
  29. if cfg.quiet:
  30. omsg_r('.')
  31. else:
  32. msg_r(f'\n password {pw_disp:9} ')
  33. ret = crypto.scrypt_hash_passphrase(pw,salt,'1').hex()
  34. assert ret == res, ret
  35. def test_presets(do_presets):
  36. for hp in do_presets:
  37. hp = str(hp)
  38. res = presets[hp]
  39. pw = 'φυβαρ'
  40. if cfg.quiet:
  41. omsg_r('.')
  42. else:
  43. msg_r(f'\n {hp!r:3}: {crypto.hash_presets[hp]!r:12} ')
  44. st = time.time()
  45. ret = crypto.scrypt_hash_passphrase(pw,salt,hp).hex()
  46. t = time.time() - st
  47. vmsg('' if cfg.test_suite_deterministic else f' {t:0.4f} secs')
  48. assert ret == res, ret
  49. if cfg.quiet:
  50. silence()
  51. cfg.force_standalone_scrypt_module = False
  52. vmsg('Passwords (auto module selection):')
  53. test_passwords()
  54. vmsg('Hash presets (auto module selection):')
  55. test_presets((1,2,3,4) if cfg.fast else (1,2,3,4,5,6,7))
  56. cfg.force_standalone_scrypt_module = True
  57. vmsg('Passwords (force standalone scrypt module):')
  58. test_passwords()
  59. vmsg('Hash presets (force standalone scrypt module):')
  60. test_presets((1,2,3))
  61. if cfg.quiet:
  62. end_silence()
  63. msg('OK')
  64. return True