views:

1457

answers:

6

Hello!

In my user database table, I take the MD5 hash of the email address of a user as the id.

Example: email([email protected]) = id(d41d8cd98f00b204e9800998ecf8427e)

Unfortunately, I have to represent the ids as integer values now - in order to be able to use an API where the id can only be an integer.

Now I'm looking for a way to encode the id into an integer for sending an decode it again when receiving. How could I do this?

My ideas so far:

  1. convert_uuencode() and convert_uudecode() for the MD5 hash
  2. replace every character of the MD5 hash by its ord() value

Which approach is better? Do you know even better ways to do this?

I hope you can help me. Thank you very much in advance!

+1  A: 

You could use hexdec to parse the hexadecimal string and store the number in the database.

Malax
Does that handle 160-bit integers without munging them?
bdonlan
answer: no, it converts to float, according to the documentation. So you'll lose about 120 bits of data, and will be unable to recover the original MD5 later.
bdonlan
You're right, the MD5 sum is too big to store it as an 32 bit integer. Ignore my answer. ;-)
Malax
Thank you Malax, it was helpful, though. :) Is hexdec($string) the same as base_convert($string, 16, 10)?
+6  A: 

Be careful. Converting the MD5s to an integer will require support for big (160-bit) integers. Chances are the API you're using will only support 32-bit integers - or worse, might be dealing with the number in floating-point. Either way, your ID will get munged. If this is the case, just assigning a second ID arbitrarily is a much better way to deal with things than trying to convert the MD5 into an integer.

However, if you are sure that the API can deal with arbitrarily large integers without trouble, you can just convert the MD5 from hexadecimal to an integer. PHP most likely does not support this built-in however, as it will try to represent it as either a 32-bit integer or a floating point; you'll probably need to use the PHP GMP library for it.

bdonlan
+1 for hinting that the resulting value might be too big for the API even when using the bare bytes as integer. You should find another solution for your "email address to integer"-problem.
Malax
Thank you very much! Would this be the solution better than my two ideas? $id_integer = base_convert($id_string, 16, 10);
Read the warning on base_convert's docs (http://www.php.net/manual/en/function.base-convert.php) - it is NOT suitable for large numbers. And MD5s are very large numbers. You _must_ use a bigint library, and the API you're accessing must do so too - but I doubt it does. Just add another column and assign arbitrary IDs to each user, it'll be much easier.
bdonlan
+1  A: 

Couldn't you just add another field that was an auto-increment int field?

SeanJA
+1  A: 

Why ord()? md5 produce normal 16-byte value, presented to you in hex for better readability. So you can't convert 16-byte value to 4 or 8 byte integer without loss. You must change some part of your algoritms to use this as id.

Alexey Sviridov
MD5 produces a 20-byte value.
bdonlan
Hmmm... may be i'm completely stupid but...fred@fred-desktop:~$ md5sum citycode.sql734e4d6f039a81c8a196db588e1cb002 citycode.sql1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1673 4e 4d 6f 03 9a 81 c8 a1 96 db 58 8e 1c b0 02here a marco92w (question owner) valued4 1d 8c d9 8f 00 b2 04 e9 80 09 98 ec f8 42 7ewhat's wrong with me? Where is additionally four bytes?
Alexey Sviridov
@bdonlan: No, 128 bits is equal to 16 bytes, isn't it?
@Alexey Sviridov: You're absolutely right, MD5 is 16 bytes. Thank you! :) I'll have to choose another way than converting MD5 to integer ...
@marco92w why you want use only md5 to produce integer by string? Why don't use your own hash function? There is some good tutorials, just google it.
Alexey Sviridov
@Alexey Sviridov: Good idea. But I think I should rather change the DB structure than start coding an own hash algorithm ... :)
The PHP function *md5()* returns a 32-character string by default. If the second parameter (which is *false* by default) is set to *true* a 16-character string is returned.
GZipp
+1  A: 

There are good reasons, stated by others, for doing it a different way.

But if what you want to do is convert an md5 hash into a string of decimal digits (which is what I think you really mean by "represent by an integer", since an md5 is already an integer in string form), and transform it back into the same md5 string:

function md5_hex_to_dec($hex_str)
{
    $arr = str_split($hex_str, 4);
    foreach ($arr as $grp) {
        $dec[] = str_pad(hexdec($grp), 5, '0', STR_PAD_LEFT);
    }
    return implode('', $dec);
}

function md5_dec_to_hex($dec_str)
{
    $arr = str_split($dec_str, 5);
    foreach ($arr as $grp) {
        $hex[] = str_pad(dechex($grp), 4, '0', STR_PAD_LEFT);
    }
    return implode('', $hex);
}

Demo:

$md5 = md5('[email protected]');
echo $md5 . '<br />';  // 23463b99b62a72f26ed677cc556c44e8
$dec = md5_hex_to_dec($md5);
echo $dec . '<br />';  // 0903015257466342942628374306682186817640
$hex = md5_dec_to_hex($dec);
echo $hex;             // 23463b99b62a72f26ed677cc556c44e8

Of course, you'd have to be careful using either string, like making sure to use them only as string type to avoid losing leading zeros, ensuring the strings are the correct lengths, etc.

GZipp
Thank you very much. This is how it would work. But now I can see what all the others wanted to say: The new integer is very long. And the leading zero is a problem, too.
Glad to help. Keep in mind that the decimal digit string and the hex digit string (the md5 string) are not equal mathematically; they are merely "translations" of each other, produced by these companion functions, into their respective digit symbol sets.
GZipp
A: 

what about:

$float = hexdec(md5('string'));

or

$int = (integer) (substr(hexdec(md5('string')),0,9)*100000000);

Definietly bigger chances for collision but still good enaugh to use instead of hash in DB though?

cheers,

/Marcin

Marcin
this one is even better: sprintf("%u",crc32(md5('string')));
Marcin