Represent MD5 hash as an integer

2019-03-15 02:31发布

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:

  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!

8条回答
Animai°情兽
2楼-- · 2019-03-15 03:07

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

查看更多
ら.Afraid
3楼-- · 2019-03-15 03:11

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:

$inthash = intval(substr(md5($str), 0, 8), 16);

For the positive value only of a signed 32-bit Int:

$inthash = intval(substr(md5($str), 0, 8), 16) >> 1;

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:

$hash = md5($str);
$inthash1 = intval(substr($hash, 0, 16), 16);
$inthash2 = intval(substr($hash, 16, 16), 16);
查看更多
别忘想泡老子
4楼-- · 2019-03-15 03:17

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.

查看更多
仙女界的扛把子
5楼-- · 2019-03-15 03:17

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

查看更多
Anthone
6楼-- · 2019-03-15 03:20

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

查看更多
闹够了就滚
7楼-- · 2019-03-15 03:27

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.

查看更多
登录 后发表回答