Skip to content

Instantly share code, notes, and snippets.

@ls0f
Last active November 10, 2015 08:49
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 ls0f/ae4e1e7432d41cb6e712 to your computer and use it in GitHub Desktop.
Save ls0f/ae4e1e7432d41cb6e712 to your computer and use it in GitHub Desktop.
gen http auth passwd file
import os
import sys
import random
# more info
# http://trac.edgewall.org/export/10770/trunk/contrib/htpasswd.py
# We need a crypt module, but Windows doesn't have one by default. Try to find
# one, and tell the user if we can't.
try:
import crypt
except ImportError:
try:
import fcrypt as crypt
except ImportError:
sys.stderr.write("Cannot find a crypt module. "
"Possibly http://carey.geek.nz/code/python-fcrypt/\n")
sys.exit(1)
def salt():
"""Returns a string of 2 randome letters"""
letters = 'abcdefghijklmnopqrstuvwxyz' \
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' \
'0123456789/.'
return random.choice(letters) + random.choice(letters)
def gen(username, password):
"""Replace the entry for the given user, or add it if new."""
pwhash = crypt.crypt(password, salt())
return "{}:{}".format(username, pwhash)
def usage():
sys.stderr.write("Usage: python nginx_auth.py username password\n")
def main():
try:
username = sys.argv[1]
password = sys.argv[2]
except IndexError:
usage()
sys.exit(1)
print gen(username, password)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment