I simply want to hash a string (a Password) in Python 3. How do I do that? What is a Simple Way of doing that? Could you please give a Coding example or Recommend me a Module that can use.
Asked
Active
Viewed 6,559 times
2 Answers
3
You can hash values in Python 3 with Hashlib:
import hashlib
h = hashlib.new('sha256')#sha256 can be replaced with diffrent algorithms
h.update('Hello World'.encode()) #give a encoded string. Makes the String to the Hash
print(h.hexdigest())#Prints the Hash

urwolfiii
- 83
- 7
-
2
-
-
-
@copycode You can see all Algorithms usable by printing out `hashlib.algorithms_available()` but I recommend sicking to sha256 because some algorithms are old/outdated. – urwolfiii Dec 27 '21 at 17:21
-1
I don't think there can be an easier way than using the built-in keyword hash
:
In [1]: hash("Hello, world!")
Out[1]: 7195831199436619873

user1717828
- 7,122
- 8
- 34
- 59
-
5should be mentioned here, that (at least in Python 3), this function does not guarantee to generate same values across processes (i.e. running `python -c "print(hash('abcde'))"` in terminal repeatedly, outputs different numbers) – peetonn Apr 04 '23 at 23:02