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.
Type Name Latest commit message Commit time
Failed to load latest commit information.

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

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
# yes, the literal string 'password' is the salt
hashed = crypt.crypt(pwd, "password")

Set the password on a user with usermod

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.