fork download
  1. import timeit
  2.  
  3. def check_password(pa, pb):
  4. # we can find the precise length of the password
  5. # by timing how much it take to return depending
  6. # on the given password length.
  7. if len(pa) != len(pb):
  8. return False
  9.  
  10. # we can guess the password, one character a time, by
  11. # timing each character guess, if the execution takes
  12. # longer time, we went one iteration further and found the correct
  13. # character.
  14. for a, b in zip(pa, pb):
  15. if a != b:
  16. return False
  17.  
  18. return True
  19.  
  20. print("timing password length:")
  21. print(timeit.timeit('check_password("0123456789", "xxx")', globals=globals()))
  22. print(timeit.timeit('check_password("0123456789", "xxxxxxx")', globals=globals()))
  23. print(timeit.timeit('check_password("0123456789", "xxxxxxxxxx")', globals=globals()))
  24. print(timeit.timeit('check_password("0123456789", "xxxxxxxxxxxxx")', globals=globals()))
  25.  
  26. print("timing amount of correct characters:")
  27. print(timeit.timeit('check_password("0123456789", "xxxxxxxxxx")', globals=globals()))
  28. print(timeit.timeit('check_password("0123456789", "0xxxxxxxxx")', globals=globals()))
  29. print(timeit.timeit('check_password("0123456789", "01xxxxxxxx")', globals=globals()))
  30. print(timeit.timeit('check_password("0123456789", "012xxxxxxx")', globals=globals()))
  31. print(timeit.timeit('check_password("0123456789", "0123xxxxxx")', globals=globals()))
  32. print(timeit.timeit('check_password("0123456789", "01234xxxxx")', globals=globals()))
Success #stdin #stdout 4.77s 9644KB
stdin
Standard input is empty
stdout
timing password length:
0.18214695900678635
0.19625090062618256
0.44359806180000305
0.18921533226966858
timing amount of correct characters:
0.45219556242227554
0.5546054244041443
0.617253877222538
0.6606327667832375
0.6955791115760803
0.7712242975831032