Python Forum
Hash command works differently for me in CMD and Spyder - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Hash command works differently for me in CMD and Spyder (/thread-20994.html)



Hash command works differently for me in CMD and Spyder - ZweiDCG - Sep-09-2019

I have a problem, my code uses the hash command to "encrypt" a number, the problem is that when I run the process through the Spyder program it gives me an X number and when I run the same code from CMD I get a completely different one. Why? Huh Huh


This is my function that encrypt my number.

import ctypes

def encriptar(number, seed):
    codigo = (number, seed)
    hash_number = ctypes.c_size_t(hash(codigo)).value
    return 'R' + str(hash_number)

number = encriptar(123456789, 123456)
print(number)
For Spyder the resulting number is 5923383283137266694
For CMD the resulting number is 1809990662

It should be noted that the version of python in Spyder is 3.7.3 and the version installed in the system is 3.7.2

Any information that I have missed to mention please tell me.

Thanks...


RE: Hash command works differently for me in CMD and Spyder - luoheng - Sep-10-2019

The hash() may return different values for the same object on different Python implementations and Python versions.
It is designed to be used only within a single Python session. So you should never rely on the value of hash() beyond this.
If you need hashing that yields the same results everywhere, use hashlib or xxhash instead.


RE: Hash command works differently for me in CMD and Spyder - ZweiDCG - Sep-10-2019

I thought that by using a seed you could have some control over the hash(), I will start using the modules you recommended, thanks.


RE: Hash command works differently for me in CMD and Spyder - DeaD_EyE - Sep-10-2019

In the same version, yes. It's used for unittests.