def set_password(self, raw_passwd): """Generates bcrypt hash and salt for storing a user's password. With bcrypt, the salt is kind of redundant, but this format stays friendly to other algorithms. """ (algorithm, salt, digest) = auth.gen_hexdigest(raw_passwd) self.password = auth.build_passwd_line(algorithm, salt, digest)
def check_password(self, raw_password): """Compares raw_password to password stored for user. Updates self.last_login on success. """ algorithm, salt, hash = auth.split_passwd_line(self.password) (_, _, user_hash) = auth.gen_hexdigest(raw_password, algorithm=algorithm, salt=salt) if hash == user_hash: self.last_login = curtime() return True else: return False
import sys import pymongo from config import DB_PORT from auth import gen_hexdigest if __name__ == '__main__': c = connection = pymongo.Connection('localhost', DB_PORT) db = c.auth users = db.users algorithm, salt, encrypted_pswd = gen_hexdigest(str(sys.argv[2])) users.insert({ 'username': sys.argv[1], 'salt': salt, 'pswd': encrypted_pswd, 'algorithm': algorithm })