我又out了.
今天本来打算改写一个python版的tinyurl函数(出处忘了..原作者见谅…),2^32 base62编码以后需要占6个字符,考虑到42亿不是很够,于是改成了2^64占11个字符.
import hashlib
def baseN(num,b):
return ((num == 0) and "0" ) or ( baseN(num // b, b).lstrip("0") + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_"[num % b])
def hash(url):
m = hashlib.md5(url)
s = m.hexdigest()
num = long(s[:16],16)^long(s[16:32],16)
return baseN(num,62)
if __name__ =="__main__":
print hash("http://www.baidu.com/")
发现php里整型只有signed int32,木有long. 搜了一把(google “php long” 第一条结果是 php Integer, FML),官方文档说:
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that’s 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
搞不了,心想那php总得有bigint吧,一查果然有个叫GMP的东东,遂搞之:
function urlhash($url){
$md5 = md5($url);
$hmd5 = gmp_init(substr($md5, 0,16),16);
$lmd5 = gmp_init(substr($md5, 16,32),16);
return gmp_strval(gmp_xor($hmd5,$lmd5),62);
}
echo urlhash("http://www.baidu.com/");
搞定。以上俩代码均输出:
FFyeAlQajro
屁H屁还真简单啊,啥都有现成的… 挺好,舒心。