D
D
Dmitry2018-05-09 20:43:13
Python
Dmitry, 2018-05-09 20:43:13

How to rewrite md5 hashing function from php to python3?

$request = array("apikey"=>$apikey,
                        "method"=>"calc",
                        "from_index"=>$from_index,
                        "to_index"=>$to_index,
                        "weight"=>$weight,
                        "ob_cennost_rub"=>$ob_cennost_rub
                    );

    if ($password != "")
    {
        //если пароль указан, аутентификация по методу API ключ + API пароль.
        $all_to_md5 = $request;
        $all_to_md5[] = $password;
        $hash = md5(implode("|", $all_to_md5));
        $request["hash"] = $hash;
    }

In the code above, everything is clear to me, except for a piece: how can I correctly rewrite this moment in python? this comes to $all_to_md5: Array ( [apikey] => string [method] => string [from_index] => 101000 [to_index] => 600000 [weight] => 1.34 [ob_cennost_rub] => 1000 [0] => string )
$hash = md5(implode("|", $all_to_md5));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
spaceatmoon, 2018-05-09
@topwebmaster

And google for what? I myself am just starting to comprehend python and this is what I found.

import hashlib

example = {"a": 1, "b": 3}
m = hashlib.md5()
res = "|".join(str(value)for key, value in example.items())
m.update(res.encode('utf-8'))
md5 = m.hexdigest()
print(md5);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question