05_md5door.py 907 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python3
  2. from hashlib import md5
  3. from itertools import count
  4. def hashes(key):
  5. for i in count(0):
  6. h = md5((key + str(i)).encode('ascii')).digest()
  7. if h[0] + h[1] == 0 and h[2] <= 0xf:
  8. yield h
  9. def door1(key):
  10. password = ''
  11. for h in hashes(key):
  12. password += hex(h[2])[-1]
  13. if len(password) == 8:
  14. print('\r', end='')
  15. return password
  16. print('\r' + password, end='')
  17. def door2(key):
  18. password = bytearray(b'\0\0\0\0\0\0\0\0')
  19. for h in hashes(key):
  20. i = h[2]
  21. if i < 8 and password[i] == 0:
  22. password[i] = ord('%x' % (h[3] >> 4))
  23. if all(password):
  24. print('\r', end='')
  25. return password.decode('ascii')
  26. print('\r' + password.decode('ascii').replace('\0', '_'), end='')
  27. print(door1('ugkcyxxp'))
  28. print(door2('ugkcyxxp'))