0

I have gone thru all the questions and answers about different hash values for same string on different platforms.But none resolved my issue.It would be helpful if I get some idea on why the below case is failing

My code:

import hashlib
import binascii

params = "{'name':'xyz-3113','sur_name':'karuna_karan_3113' ,'init_range':'500','power_down_range':'0','power_high_range':'1000'}"
name = 'xyz'

def generateHash(name,paramsDict={}):

    paramsDict = eval(paramsDict)

    key = hashlib.md5(str(name)+str(paramsDict))
    bin_key = key.digest()
    return bin_key

hash_key = generateHash(name, params)
print binascii.hexlify(hash_key)

Output in Windows: ea94e618b69f10d55dcd27562fb06378

Output in Linux: 6d1a40ae190c63f687456a46321165e9

2
  • 2
    Depending on the Python version there is no guarantee for the order of a dict and therefore the hash will differ. Also the eval doesn't make any sense in your code. You could just use a dict directly. Commented Dec 27, 2019 at 8:29
  • Nice observation OP! Now that behavior is expected if not enforced by the language itself. What are you trying to achieve though? Depending on many factors those hashes will change from machine to machine. OS to OS.. if it's a 64-bit machine or 32-bit machine. What seems to be your issue? What are you trying to solve? Commented Dec 27, 2019 at 9:53

2 Answers 2

2

It is more a difference between different Python versions. E.g. on my macOS I get different results between Python 2 and Python 3.

In Python 2:

eval(params)
{'power_high_range': '1000', 'power_down_range': '0', 'sur_name': 'karuna_karan_3113', 'name': 'xyz-3113', 'init_range': '500'}

In Python 3:

eval(params)                                                                                                        
{'name': 'xyz-3113', 'sur_name': 'karuna_karan_3113', 'init_range': '500', 'power_down_range': '0', 'power_high_range': '1000'}

The order of the fields in dict is different, so str(paramsDict) is different and the result is different. Note that even though Python 3 preserves the order, it is considered implementation details and one should not rely on it. Sort the members instead to guarantee specific order.

Sign up to request clarification or add additional context in comments.

4 Comments

Both Python versions are same and the OS bits as well.I agree with your suggestion.Do I need to sort the dict by key?
@Nithya you could use an OrderedDict from the collections-module
@Nithya Then I guess your problem is not just the ordering of keys, since the OrderedDict would have handled that
May be.If I can modify the dict by adding new element or just removing 'power' from the key its working.Facing very strange behavior and hitting my head
1

As others have observed, the problem most likely relates to the ordering of the dict items. This is one way to address it:

def generate_hash(name, paramsDict):
    strd = ''.join([str(k) + str(v) for k, v in sorted(paramsDict.items())])
    key = hashlib.md5(str(name) + strd)
    bin_key = key.digest()
    return bin_key

As well as explicitly sorting the dict items, I've removed the default value of the paramsDict argument, as this can lead to unexpected behaviour.

If the raw digest value is not used outside the function I'd suggest returning key.hexdigest() to avoid the need for importing binascii.

1 Comment

..Excellent!!!..its simply fixed the issue.Great!!.Thanks :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.