Skip to content

Instantly share code, notes, and snippets.

@tkhn
Created September 10, 2013 17:56
Show Gist options
  • Select an option

  • Save tkhn/6513072 to your computer and use it in GitHub Desktop.

Select an option

Save tkhn/6513072 to your computer and use it in GitHub Desktop.
Simple console htpasswd hash generator
#!/usr/bin/python
"""Replacement for htpasswd"""
# Original author: Eli Carter
# Inspired by https://gist.github.com/eculver/1420227
import crypt
import os
import sys
import random
from optparse import OptionParser
def salt():
"""Returns a string of 2 randome letters"""
letters = 'abcdefghijklmnopqrstuvwxyz' \
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' \
'0123456789/.'
return random.choice(letters) + random.choice(letters)
def main():
"""%prog username password
Generate passwd hash"""
parser = OptionParser(usage=main.__doc__)
options, args = parser.parse_args()
# Non-option arguments
if len(args) != 2:
syntax_error("Insufficient number of arguments.\n")
else:
username = args[0]
password = args[1]
print "{0}:{1}".format(username, crypt.crypt(password, salt()))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment