homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

generate a unique number from a string

 7:31 pm on Aug 6, 2010 (gmt 0)

Here's a neat problem. I love it when I get problems like this one, they make my job worthwhile.

I have a system with users, each of which have a unique name (a string).
For each user in our system, we need a "passcode" - it's like a numeric password.

The pass code must be as unique as their name. like, a one-way cipher that will always turn "Bob" into "3241" and "Mary" into "5588".

What is the fastest, MOST ELEGANT way to do this? In PHP, of course.

Just for aesthetic reasons, it should be 4 or 5 digits. Any less and they won't think it's secure enough, any more then it'll be hard to remember.

Do I convert the text to decimal? extract the digits from an MD5? Employ a substitution cipher on each character using array_walk()? seed a rand() using the sum of the string's ord() values?

I'm looking for something elegant here



 7:33 pm on Aug 6, 2010 (gmt 0)

correction - the number does not have to be "unique". the string is already unique, and there are more than 99999 of them, so I know I'm going to get duplicate numbers that apply to more than one string.

that's OK.


 7:43 pm on Aug 6, 2010 (gmt 0)

Does it have to be a base10 number? If not, MD5 is designed exactly for what you want. MD5 returns the hash as a 32-character hexadecimal number.

$str = 'apple';

if (md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
echo "Would you like a green or red apple?";

If it needs to be base10, you could add an extra function/layer around md5 that maps the hex a through f to base 10 numbers. a=1, b=2, etc.


 8:10 pm on Aug 6, 2010 (gmt 0)

yes, it has to be a base 10 number

I should have mentioned that

how about:

$number = base_convert(md5($string), 16, 10) % 9999 ;


 8:29 pm on Aug 6, 2010 (gmt 0)

The MD5 converted to base 10 was a huge unique number. But then that number modulo 9999 was always the same: 5947

that's because the maximum integer in PHP is 9223372036854775807.
The base-10 of an MD5 is greater than that number.

9223372036854775807 % 9999 = 5947

Funny that PHP can do base conversion and come up with this gargantuan number, but then it can't perform simple math operations on it. not unless I haul out the BC functions, which I only do when precision is crucial.

This did a nice job:

substr(base_convert(md5($string), 16, 10) , -5);

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved