请帮助我获得准确的md5的这个python脚本的PHP值
Python脚本
def md5code(params):
params = {'identifier': ' ', 'amount': '0.0', 'code': 'UNIA'}
req = dict([[key, params.get(key, '')] for key in ['code', 'identifier', 'amount']])
secret = '2fd0bba6b1774ed391c1ff8467f52a5d'
text = ":".join([req[x] for x in ['code', 'identifier', 'amount']] + secret)
return md5(text).hexdigest().upper()返回值为: 5D316CD2311678A1B12F6152988F3097
PHP脚本
$secret = '2fd0bba6b1774ed391c1ff8467f52a5d';
$code = 'UNIA';
$valid_institution = array('amount' => '0.0', 'code' => $code, 'identifier' => ' ');
foreach($valid_institution as $k => $v) {
$text = implode(":", $v[$k] + $secret);
}
print strtoupper(hash("md5", $text)); 返回值为: D41D8CD98F00B204E9800998ECF8427E
我期望PHP脚本返回一个确切的md5值,但事实并非如此。
任何建议都将不胜感激。
谢谢
发布于 2011-09-30 01:57:11
好的,看起来你使用foreach的方式是错误的。试试这个:
$secret = '2fd0bba6b1774ed391c1ff8467f52a5d';
$code = 'UNIA';
$valid_institution = array('amount' => '0.0', 'code' => $code, 'identifier' => ' ');
$text =
$valid_institution['code'] . ":" .
$valid_institution['identifier'] . ":" .
$valid_institution['amount'] . ":" .
$secret;
print strtoupper(hash("md5", $text)); 发布于 2011-09-30 02:00:41
有效的Python代码可能如下所示:
from hashlib import md5
params = {'identifier': ' ', 'amount': '0.0', 'code': 'UNIA'}
req = dict([[key, params.get(key, '')] for key in ['code', 'identifier', 'amount']])
secret = '2fd0bba6b1774ed391c1ff8467f52a5d'
text = ":".join(req[x] for x in ['code', 'identifier', 'amount']) + secret
print md5(text).hexdigest().upper()这是相当于PHP的
<?php
$secret = '2fd0bba6b1774ed391c1ff8467f52a5d';
$code = 'UNIA';
$valid_institution = array(
'code' => $code,
'identifier' => ' ',
'amount' => '0.0');
$text = implode(':', $valid_institution) . $secret;
print strtoupper(hash("md5", $text));
?>发布于 2011-09-30 01:59:28
$secret = '2fd0bba6b1774ed391c1ff8467f52a5d';
$code = 'UNIA';
$valid_institution = array('amount' => '0.0', 'code' => $code, 'identifier' => ' ');
$text = implode(":", $valid_institution). $secret;
print strtoupper(hash("md5", $text)); https://stackoverflow.com/questions/7601223
复制相似问题