Created
February 19, 2017 06:36
-
-
Save Izzette/5e45c9dc8ce4b51f2aa4b2ab8e83765c to your computer and use it in GitHub Desktop.
Create encrypted Unix password in Python
This file contains 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/env python | |
import string | |
from random import SystemRandom | |
from crypt import crypt | |
from getpass import getpass | |
def get_salt(saltlen): | |
copts = string.ascii_letters + string.digits | |
sysrand = SystemRandom() | |
salt = "" | |
for i in range(saltlen): | |
salt += sysrand.choice(copts) | |
return salt | |
def input_passwd(): | |
pw, conf = getpass("Password: "), getpass("Confirm: ") | |
if pw != conf: | |
print("Does not match!") | |
exit(1) | |
return pw | |
def crypt_passwd(pw, salt): | |
return crypt(pw, "$6${0}$".format(salt)) | |
def main(): | |
salt = get_salt(8) | |
pw = input_passwd() | |
cpw = crypt_passwd(pw, salt) | |
print(cpw) | |
main() | |
# vim: set ts=4 sw=4 et syn=python |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment