Represent MD5 hash as an integer

前端 未结 8 1907
Happy的楠姐
Happy的楠姐 2021-02-05 09:42

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)

相关标签:
8条回答
  • 2021-02-05 10:26

    what about:

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

    or

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

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

    0 讨论(0)
  • 2021-02-05 10:29

    Be careful. Converting the MD5s to an integer will require support for big (128-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.

    0 讨论(0)
提交回复
热议问题