Skip to content

Instantly share code, notes, and snippets.

@Izzette
Created February 19, 2017 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Izzette/5e45c9dc8ce4b51f2aa4b2ab8e83765c to your computer and use it in GitHub Desktop.
Save Izzette/5e45c9dc8ce4b51f2aa4b2ab8e83765c to your computer and use it in GitHub Desktop.
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!")
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