Created
January 9, 2014 05:26
-
-
Save garrettreid/8329796 to your computer and use it in GitHub Desktop.
Create a salted SHA512 password hash for use with Dovecot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import hashlib | |
import getpass | |
import base64 | |
password1 = None | |
password2 = None | |
# Read the password | |
while password1 != password2 or password1 == None: | |
password1 = getpass.getpass() | |
password2 = getpass.getpass("Confirm password: ") | |
if (password1 != password2): | |
print "\nPassword mismatch, try again." | |
# Generate a 5 byte random salt | |
salt = os.urandom(5) | |
# Hash our password + salt | |
sha = hashlib.sha512() | |
sha.update(password1) | |
sha.update(salt) | |
ssha512 = base64.b64encode('{}{}'.format(sha.digest(), salt)) | |
# Print it out with a prefix for Dovecot | |
print "\n{{SSHA512}}{}".format(ssha512) |
You should use #!/usr/bin/python2
I made a golang equivalent (without the readline) at https://gist.github.com/osiloke/186a0feb6e203afa01c11d43439a383f
Updated for python3: https://gist.github.com/JayBeavers/7bca0642f591daab0d0916dbcdd4e3b3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks alot for this snippet 👍
Tested it with
doveadm pw -t $YOUR_GENERATED_HASH -p $PASSWORD
and it worked perfectly.