drecute
drecute

Reputation: 758

Need a PHP script to return an exact match of a python script

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

Upvotes: 0

Views: 414

Answers (3)

Jochen Ritzel
Jochen Ritzel

Reputation: 107706

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

Upvotes: 1

RiaD
RiaD

Reputation: 47660

$secret = '2fd0bba6b1774ed391c1ff8467f52a5d';
$code = 'UNIA';
$valid_institution = array('amount' => '0.0', 'code' => $code, 'identifier' => ' ');
$text = implode(":", $valid_institution). $secret;
print strtoupper(hash("md5", $text)); 

Upvotes: 0

user187291
user187291

Reputation: 53950

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

Upvotes: 2

Related Questions