Buboon
Buboon

Reputation: 405

What is the PHP analogue of the md5 function in Perl?

I have a Perl script and I need to convert it to PHP. What is PHP analogue of the md5 function in Perl?

Perl script:

$hash  = md5($str1, $str2); 

PHP script:

 $hash  = md5($str1.$str2);

I have different values in $hash. How I can get same value of $hash in PHP?

Thx.

Upvotes: 3

Views: 256

Answers (2)

ikegami
ikegami

Reputation: 386706

It's simply

$hash  = md5($str1.$str2, true);

You claim it's not equivalent, but the following shows it is:

$ 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
identical

Upvotes: 1

leeor
leeor

Reputation: 17811

Looks like the perl version you are using outputs in binary format:

http://perldoc.perl.org/Digest/MD5.html

md5($data,...)

This function will concatenate all arguments, calculate the MD5 digest of this "message", and return it in binary form. The returned string will be 16 bytes long.

Try this in PHP:

$hash  = md5($str1.$str2, true);

See the php docs for details.

Upvotes: 5

Related Questions