我有一个Perl脚本,我需要将它转换成PHP。在Perl中,md5函数的PHP模拟是什么?
Perl脚本:
$hash = md5($str1, $str2); PHP脚本:
$hash = md5($str1.$str2);我在$hash中有不同的值。如何在PHP中获得相同的$hash值?
谢谢。
发布于 2015-09-18 07:40:19
看起来像您使用二进制格式的输出的perl版本:
http://perldoc.perl.org/Digest/MD5.html
md5($data,...)该函数将连接所有参数,计算此“消息”的MD5摘要,并以二进制形式返回它。返回的字符串将有16个字节长。
在PHP中试试这个:
$hash = md5($str1.$str2, true);有关详细信息,请参阅php docs。
发布于 2015-09-18 14:59:22
很简单
$hash = md5($str1.$str2, true);您声称它不是等价的,但下面的说明说明了这一点:
$ cat x.pl
use Digest::MD5 qw( md5 );
my $str1 = join '', map chr, 0x00..0x7F;
my $str2 = join '', map chr, 0x80..0xFF;
print md5($str1, $str2);
$ perl x.pl | od -t x1
0000000 e2 c8 65 db 41 62 be d9 63 bf aa 9e f6 ac 18 f0
0000020$ cat x.php
<?php
$str1 = join('', array_map("chr", range(0x00, 0x7F)));
$str2 = join('', array_map("chr", range(0x80, 0xFF)));
echo md5($str1.$str2, true);
?>
$ php x.php | od -t x1
0000000 e2 c8 65 db 41 62 be d9 63 bf aa 9e f6 ac 18 f0
0000020$ diff -q <( perl x.pl ) <( php x.php ) && echo identical || echo different
identicalhttps://stackoverflow.com/questions/32646360
复制相似问题