From d8a7a022df815d8254c4a9674ef1143f50caa277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 28 Nov 2022 19:34:35 +0100 Subject: Documentation. --- password.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'password.py') diff --git a/password.py b/password.py index 16192be..ff0df21 100755 --- a/password.py +++ b/password.py @@ -1,4 +1,11 @@ #!/usr/bin/env python3 + +""" +Simple password store, backed by a JSON file. + +Also contains an entry point for managing the store. +""" + import hashlib import json import os @@ -28,6 +35,12 @@ class PasswordEntry(TypedDict): class Passwords: + """ + Simple password store. + + [Parameters] + fname - Path of json file to load and store from. + """ def __init__(self, fname: os.PathLike): self.fname = fname self.db: dict[str, PasswordEntry] @@ -38,6 +51,7 @@ class Passwords: self.db = {} def save(self) -> None: + """Dump current data to disk.""" try: with open(os.fspath(self.fname) + '.tmp', 'w') as f: json.dump(self.db, f) @@ -47,6 +61,7 @@ class Passwords: print(f'Saving password failed {e}') def add(self, username: str, password: str) -> None: + """Add (or modify) entry in store.""" if cur := self.db.get(username): salt = cur['salt'] hashed = hashlib.sha256((salt + password).encode('UTF-8')) @@ -65,6 +80,7 @@ class Passwords: } def validate(self, username: str, password: str) -> bool: + """Check if user exists, and if it has a correct password.""" # These shall fail when key is missing data = self.db[username] proc = hash_methods[data['method']] @@ -72,8 +88,7 @@ class Passwords: return data['hash'] == digest -if __name__ == '__main__': - +def main(): import argparse parser = argparse.ArgumentParser() @@ -99,3 +114,7 @@ if __name__ == '__main__': print(passwords.validate(args.username, args.password)) else: parser.print_help() + + +if __name__ == '__main__': + main() -- cgit v1.2.3