将MD5转换为base62以获取URL [英] Convert MD5 to base62 for URL
问题描述
我有一个脚本可以转换为以62为基数(A-Za-z0-9),但是如何从MD5中获取一个数字?
I have a script to convert to base 62 (A-Za-z0-9) but how do I get a number out of MD5?
我在很多地方都读到,因为MD5中的数字大于php可以处理的整数,所以它是不准确的...因为我还是想要一个简短的URL,并且不打算使用整个哈希值,也许它只有8个字符....
I have read in many places that because the number from an MD5 is bigger than php can handle as an integer it will be inaccurate... As I want a short URL anyway and was not planning on using the whole hash, maybe just 8 characters of it....
所以我的问题是如何获取MD5哈希值的一部分?
So my question is how to get part of the number of an MD5 hash?
仅使用MD5哈希的一部分也是一个坏主意吗?
Also is it a bad idea to use only part of the MD5 hash?
推荐答案
在这里,我将提出另一种建议..由于您只对使用md5哈希的十进制块感兴趣,所以为什么不使用任何其他简短的数字哈希,例如 CRC32 或
I'm going to suggest a different thing here.. Since you are only interested in using a decimal chunk of the md5 hash why don't you use any other short numeric hash like CRC32 or Adler? Here is an example:
$hash = sprintf('%u', crc32('your string here'));
这将为您的字符串生成8位数字的哈希值.
This will produce a 8 digit hash of your string.
我想我误解了您,这里是一些功能,可提供往返于62的基数的转换..
I think I misunderstood you, here are some functions that provide conversions to and from bases up to 62.
编辑(再次):要使用任意长度的数字,您必须使用bc_math或GMP扩展名,
EDIT (Again): To work with arbitrary length numbers you must use either the bc_math or the GMP extension, here is a function that uses the bc_math extension and can also convert from base 2 up to base 62. You should use it like this:
echo bc_base_convert(md5('your url here'), 16, 62); // public base 62 hash
以及反函数:
echo bc_base_convert('base 62 encoded value here', 62, 16); // private md5 hash
希望它会有所帮助. =)
Hope it helps. =)
这篇关于将MD5转换为base62以获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!