Remix.run Logo
teo_zero 2 days ago

While I generally like to reinvent the wheel, for hash functions I strongly recommend to use a proved good one. Djb2 by the venerable Daniel Bernstein satisfies all the requirements of TFA.

  h = 5381
  while still has data:
    h = h * 33 + next_byte()
  return h
PS of course if you think the multiplication is overkill, consider that it is nothing more than a shift and an addition.
purplesyringa 2 days ago | parent [-]

Djb2 is hardly a proven good hash :) It's really easy to find collisions for it, and it's not seeded, so you're kind of screwed regardless. It's the odd middle ground between "safely usable in practice" and "fast in practice", which turns out to be "neither safe nor fast" in this case.