Skip to content

Instantly share code, notes, and snippets.

@cypher
Forked from k0d/gist:2394453
Created April 15, 2012 19:42
Show Gist options
  • Save cypher/2394473 to your computer and use it in GitHub Desktop.
Save cypher/2394473 to your computer and use it in GitHub Desktop.
htpasswd check
import crypt
def find_line_in_file(filename, string_to_find):
with open(filename, 'r') as f:
for line in f:
if line.startswith(string_to_find):
yield line
def check(username, password):
for line in find_line_in_file('/srv/htpasswd', username+':'):
user, t, salt, hashed = line.rstrip('\n').split('$')
crypted_password = crypt.crypt(password, '$6$%s$' % salt)
existing_password = '$6$%s$%s' % (salt, hashed)
return crypted_password == existing_password
# Couldn't find the user
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment