Implementing SHA in a real-world scenario
The following is an example of real-life SHA implementation.
Getting ready
For this script, we will need the hashlib library and the uuid library.
How to do it…
For this real-world example, we will be implementing an SHA256 encoding scheme and generating a salt to make it even more secure by defeating precomputed hash tables. We will then run it through password-checking to ensure the password was typed correctly:
#!/usr/bin/python
import uuid
import hashlib
# Let's do the hashing. We create a salt and append it to the password once hashes.
def hash(password):
salt = uuid.uuid4().hex
return hashlib.sha512(salt.encode() + password.encode()).hexdigest() + ':' + salt
# Let's confirm that worked as intended.
def check(hashed, p2):
password, salt = hashed.split(':')
return password == hashlib.sha512(salt.encode() + p2.encode()).hexdigest()
password = raw_input('Please enter a password: ')
hashed = hash(password)
print('The string to store...