php hash form string to integer

♀尐吖头ヾ 提交于 2019-12-18 04:34:31

问题


Does PHP have a built in function for doing string to integer hashes, something that's difficult to reverse?

Now, I know I can probably get away with doing an md5, and treating a substring of it as a radix 16 number, but I'm looking for something built in.

Thanks.


回答1:


I think the best bet would chose a standard hash [either md5() or sha1()] to obtain a hash of your string, and then to get an integer hash, to a base_convert($hash, 16, 10) and that should convert your hash into a integer hash.

Hope I am understanding your issue correctly.




回答2:


I think you are on the right path in approaching this problem in two steps.

First, you should probably stick with the md5 hash to fulfill your "difficult to reverse" requirement.

Second, you could take the md5 output as input to your "convert this to an integer" function.

for the second part, what are you going after exactly? Does it have to be an integer? Or just printable characters? if you are just looking to convert your hash into something you can store in a database, transmit over the wire, or some other reason the md5 string won't do, the convertuuencode function might work for you: http://us.php.net/manual/en/function.convert-uuencode.php

Another roundabout hackish approach would be to get the binary value of your hash, and convert it to a decimal using: http://us.php.net/manual/en/function.bindec.php although, i've never tried this and am not sure if it would work like you want it to.




回答3:


I don't think you will find anything builtin for that, but your idea with md5() is pretty good, actually. I couldn't imagine why you would need something else: couldn't be faster, couldn't be more stable, ...



来源:https://stackoverflow.com/questions/965304/php-hash-form-string-to-integer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!