Is there a way to find a collision for a given hash function without brute forcing? The particular hash function I'm talking about is the one used by Python (simplified version given below). It returns a 64-bit value in my case, so I'd guess on average I need to try 2^32 values to get a collision. But given that we know the function is there a way to predict two values for which h(x1) == h(x2)?

static long string_hash(PyStringObject *a) {
  len = Py_SIZE(a);                 //len is length of string
  p = (unsigned char *) a->ob_sval; //p is the string to hash
  x = *p << 7;
  while (--len >= 0)
    x = (1000003*x) ^ *p++;
  x ^= Py_SIZE(a);
  return x;
  • $\begingroup$ In general you can only do this by careful analysis of the particular hash function. In some cases they are designed specifically to make this very hard. I don't know if this is true for the python hash function. You could try asking the person who wrote the code. $\endgroup$ – felix Oct 17 '13 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.