Skip to content

Instantly share code, notes, and snippets.

@Izzette Izzette/
Created Feb 19, 2017

What would you like to do?
Create encrypted Unix password in Python
#!/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!")
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)
# 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
You can’t perform that action at this time.