From anon, 1 Year ago, written in Plain Text.
Embed
  1. from itertools import chain, product
  2. import string
  3. def bruteforce(charset, maxlength):
  4.     return (''.join(candidate)
  5.         for candidate in chain.from_iterable(product(charset, repeat=i)
  6.         for i in range(1, maxlength + 1)))
  7. def java_string_hashcode(s):
  8.     h = 0
  9.     for c in s:
  10.         h = (31 * h + ord(c)) & 0xFFFFFFFF
  11.     return ((h + 0x80000000) & 0xFFFFFFFF) - 0x80000000
  12. pw = 'Pas$ion'
  13. wanted_hash = java_string_hashcode(pw)
  14. print('{} has hash {}'.format(pw, wanted_hash))
  15. for attempt in bruteforce(string.ascii_letters + string.digits, 7):
  16.     if java_string_hashcode(attempt) == wanted_hash:
  17.         print('Found collision with {}'.format(attempt))
  18.         break