Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md

README.md

page title tags date
Pre-Hashing Passwords In Python
python mysql linux
2015-11-27

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}';
You can’t perform that action at this time.