Skip to content
Advertisement

PHP Generate an unique hash from an IPADDRESS

I have a board and i want to disable registrations instead i want the users to be able to post freely with an unique ID based on their IPAddress

for example:

if “Joe” IP is 200.100.15.117, “Joe” nickname will become the hash function of 200.100.15.117

if “Joe” IP changes he will get another ID, that doesn’t matter, i only want one unique ID per IPAddress

There are also two important things :

  • unique ID must be 8 characters long
  • hash should be secure, i don’t want hackers be abble to crack my users IPaddresses

I thought in using an MD5 function and then trim it to 8 chars, but how unique is that ? is there a better way ?

Advertisement

Answer

EDIT According to OP comment on this answer.

Code below uses BC Math library to translate a MD5 hash into a Base-90 string. This converts a 32 char string into a 20 char one. This is far from desired 8 chars but it is the minimum string length using ASCII range (it is possible to increase the base to Base-94 by using chars ' " [space] but this does not affect to the string length and instead may cause problems while handling data).

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement