-
-
Save cypher/2394473 to your computer and use it in GitHub Desktop.
htpasswd check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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