Project Page: github/prehash-passwords

Hashing passwords beforehand which will be used in scripts or other places can be useful. It allows you to store the hashed value for later use, but keep the original password securely stored elsewhere. Here’s how to hash passwords in Python for Unix user accounts and MySQL users.

Unix Password

import crypt
pwd = "MY SECURE PASSWORD"
# yes, the literal string 'password' is the salt
hashed = crypt.crypt(pwd, "password")
print(hashed)

Set the password on a user with usermod

PASSWORD=$(./codefromabove.py)
usermod --password "$PASSWORD" myuser

MySQL Password

import hashlib
# mysql pwd is two iterations of sha1
first = hashlib.sha1(pwd.encode('ascii'))
second = hashlib.sha1(first.digest())
return "*" + second.hexdigest().upper()

Apply it to a user in mysql query

CREATE USER 'myuser'@'localhost' IDENTIFIED BY PASSWORD '{hashed_value_goes_here}';