将MD5散列表示为整数 [英] Represent MD5 hash as an integer

查看:145
本文介绍了将MD5散列表示为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的用户数据库表中,我将用户电子邮件地址的MD5散列作为id。



示例: email(不幸的是,我不得不将id表示为整数值 - 为了成为能够使用API​​,其中id只能是一个整数。



现在我正在寻找一种方法将id编码为一个整数,以便再次发送解码当收到。



我的想法到目前为止:


  1. convert_uuencode() convert_uudecode()为MD5哈希

  2. 替换每个字符MD5散列通过它的 ord()

哪种方法更好?你知道更好的方法吗?



我希望你能帮助我。非常感谢您的提前! 解决方案

小心。将MD5转换为整数将需要支持大(128位)整数。机会是你使用的API只支持32位整数 - 或者更糟的是,可能是在处理浮点数。无论哪种方式,您的身份证将会消失。如果是这种情况,只是任意分配第二个ID是处理事情的好方法,而不是试图将MD5转换为整数。



然而,如果你肯定 API可以毫无问题地处理任意大的整数,您可以将MD5从十六进制转换为整数。然而,PHP很可能不支持这种内置,因为它会尝试将其表示为32位整数或浮点;您可能需要使用 PHP GMP库


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!

解决方案

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.

这篇关于将MD5散列表示为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆