我需要生成相同的由PHP生成的adler32哈希,但使用Python。不幸的是,標準實現不同。有任何想法嗎?如何在Python中獲得相同的PHP adler32哈希值?
$ php -r 'print hash("adler32", "bla") . "\n";'
02620130
$ python -c 'import zlib; print zlib.adler32("bla");'
39977264
(我使用PHP 5.5.9和Python 2.7.6)
在我使出
def php_adler32(string):
phpcode = """print hash("adler32", "%s");""" % string
try:
rv = subprocess.check_output(['php','-r',phpcode], shell=False)
except subprocess.CalledProcessError as e:
raise RuntimeError("Could not compute adler32 through php: %s" % e)
return rv
的時刻,但恐怕要結束了對dailywtf.com與此代碼。
謝謝!爲了將來的參考,這相當於PHP:'「%08d」%int(hex(adler32('bla'))[2:]' – Willem