CodeIgniter Forums
How to create a short hash - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to create a short hash (/showthread.php?tid=3226)



How to create a short hash - El Forum - 09-18-2007

[eluser]dmorin[/eluser]
So here's the deal. I'm creating a site where I send an email and they need to confirm something by clicking a link. But rather then send an MD5 hash as the unique value used to determine that they received the email, I would like to send a shorter random string of maybe 6-8 characters. What's the best way to do this? Should I just calculate an MD5 hash and then grab the first 6 characters and use there, or is there a better way?

I'm aware that by shortening it, my chances for duplicates will be much higher. I'll check the list of existing ones before committing to using one.

If anyone has any ideas of how to better calculate these short hashes/random strings, please let me know. Thanks.


How to create a short hash - El Forum - 09-18-2007

[eluser]Michael Wales[/eluser]
Wow, talk about timing.

About a month ago I wrote an article entitled "Handling Forgotten Passwords" in which I discuss various methods of allowing a user to retrieve/reset a lost password. One of the common themes amongst all of these methods was an emailed verification/activation code - this also happened to be one of the most frequent questions I recieved.

Yesterday I posted an article "Generating Random Strings" which takes a 10-line function I originally suggested (to just generate the random string) to 9-line function, which generates the random string, checks it for duplication, and updates the user's record if it's valid.

Check it out - I'm sure both article will help.


How to create a short hash - El Forum - 09-18-2007

[eluser]dmorin[/eluser]
Perfect, thanks for the response. I didn't know about the random_string() function. That will be a big help.

Thanks again.


How to create a short hash - El Forum - 08-28-2008

[eluser]Jesse Schutt[/eluser]
Michael,

Do you happen to have updated links for those articles?

Jesse


How to create a short hash - El Forum - 08-28-2008

[eluser]Lucas3677[/eluser]
How about substr(md5(microtime()), 0, 6)?


How to create a short hash - El Forum - 08-29-2008

[eluser]Thorpe Obazee[/eluser]
Correct me if I am wrong, but shouldn't uniqid() enough for this?