In my user database table, I take the MD5 hash of the email address of a user as the id.
Example: email(example@example.org) = 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:
convert_uuencode()
andconvert_uudecode()
for the MD5 hash- 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!
You could use hexdec to parse the hexadecimal string and store the number in the database.
For a 32-bit condensation, a simple solution could be made by selecting 4 hex pairs (8 characters) of the MD5 hash, where each pair represents one byte, and then converting that with
intval()
.For an unsigned 32-bit Int:
For the positive value only of a signed 32-bit Int:
This will likely only work for values up to 64-bit (8 bytes or 16 characters) for most modern systems as noted in the docs.
On a system that can accommodate 64-bit Ints, a splitting strategy that consumes the entire 128-bit MD5 hash as 2 Ints might look like:
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.
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
Couldn't you just add another field that was an auto-increment int field?
Use the email address as the file name of a blank, temporary file in a shared folder, like /var/myprocess/example@example.org
Then, call ftok on the file name. ftok will return a unique, integer ID.
It won't be guaranteed to be unique though, but it will probably suffice for your API.