D
D
dacdacdac12018-06-30 15:03:53
Python
dacdacdac1, 2018-06-30 15:03:53

Python md5 hash not matching?

Hello.
There is a kind of php code:

$salt = "21x1231";
$code = file_get_contetns('api...');
$code = json_decode($code, true);
$token = $code->token;

$hash = strtolower(md5($token.salt)); 
$data = file_get_contents('url?param='.$token.'&tok='.$hash);

rewritten in python:
import hashlib
import urllib
import json

salt = "21x1231".encode()
code_request = urllib.request.urlopen('api..').read()
code = json.loads(code_request)
hash = hashlib.md5(code['token'].encode() + salt).hexdigest()
rq = urllib.request.urlopen("api?param=%s&tok=%s" % (code['token'], hash.lower())).read()

But it returns a message that the hash does not match. Although a similar code works in php, the data all comes as it should (look in the debug), the problem is with the hash or with the last request where it is passed to the url, what could be the problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DevMan, 2018-06-30
@dacdacdac1

php code is complete trash, which does not work at all:

  • json_decode($code, true) returns an array, but $code->token is being treated as an object.
  • $token.salt is what?
$salt = "21x1231";
$code = file_get_contetns('api...');
$code = json_decode($code);
$token = $code->token;

$hash = strtolower(md5($token.$salt)); 
$data = file_get_contents('url?param='.$token.'&tok='.$hash);

php - https://ideone.com/OAzxQ1
python - https://ideone.com/sQtDLp

A
Andrey, 2018-06-30
@VladimirAndreev

In md5, one argument is passed in both cases?

L
lega, 2018-06-30
@lega

Your code does different things, do a similar algorithm - the result will be the same, md5 has nothing to do with it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question