需要 PHP 脚本返回 Python 脚本的精确匹配

发布于 2024-12-07 09:56:24 字数 926 浏览 0 评论 0原文

请我需要帮助在 PHP 中获取此 python 脚本的准确 md5 值

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 值,但它是不是。

任何建议将不胜感激。

谢谢

Please I need help getting the exact md5 value of this python script in PHP

The Python script

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()

The return value is: 5D316CD2311678A1B12F6152988F3097

The PHP script

$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)); 

The return value is: D41D8CD98F00B204E9800998ECF8427E

I'm expecting the PHP script to return an exact md5 value, but it is not.

Any suggestions will be appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山人契 2024-12-14 09:56:24

好吧,看来您使用 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)); 

ok, seems you're using foreach in a wrong way. Try this:

$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)); 
刘备忘录 2024-12-14 09:56:24

有效的 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)); 
?>

Valid Python code could look like this:

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()

and this is the equivalent 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)); 
?>
凉风有信 2024-12-14 09:56:24
$secret = '2fd0bba6b1774ed391c1ff8467f52a5d';
$code = 'UNIA';
$valid_institution = array('amount' => '0.0', 'code' => $code, 'identifier' => ' ');
$text = implode(":", $valid_institution). $secret;
print strtoupper(hash("md5", $text)); 
$secret = '2fd0bba6b1774ed391c1ff8467f52a5d';
$code = 'UNIA';
$valid_institution = array('amount' => '0.0', 'code' => $code, 'identifier' => ' ');
$text = implode(":", $valid_institution). $secret;
print strtoupper(hash("md5", $text)); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文