Advertisement
Guest User

similar-hash.py

a guest
Jun 6th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import hashlib
  4.  
  5. original = 'Install the program the user expects and exit.'.encode('ascii')
  6. target   = 'Install a backdoor and exit. '.encode('ascii')
  7.  
  8. def get_score(hash1, hash2):
  9.     assert len(hash1) == len(hash2)
  10.  
  11.     start = 0
  12.     while start < len(hash1) and hash1[start] == hash2[start]:
  13.         start += 1
  14.  
  15.     end = 0
  16.     while end < len(hash1) and hash1[-end-1] == hash2[-end-1]:
  17.         end += 1
  18.  
  19.     similar = 0
  20.     for c1, c2 in zip(hash1, hash2):
  21.         if (c1 <= '9') == (c2 <= '9'):
  22.             similar += 1
  23.  
  24.     score = min(start, end) + (start + end) / 4.0 + similar / 10.0
  25.  
  26.     return score
  27.  
  28. original_hash = hashlib.md5(original).hexdigest()
  29.  
  30. space = len(original) - len(target)
  31.  
  32. nonce = 0
  33. best_score = -1
  34.  
  35. while True:
  36.     candidate = target + str(nonce).rjust(space, '0').encode('ascii')
  37.     candidate_hash = hashlib.md5(candidate).hexdigest()
  38.  
  39.     score = get_score(original_hash, candidate_hash)
  40.     if score > best_score:
  41.         best_score = score
  42.         print('{0}: {1}'.format(original, original_hash))
  43.         print('{0}: {1}'.format(candidate, candidate_hash))
  44.  
  45.     nonce += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement