From ae57a2d2aebd8ed39f788dd84b3484c2f06fa969 Mon Sep 17 00:00:00 2001 From: jay23606 <4602056+jay23606@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:07:58 -0500 Subject: [PATCH] Add hash command to print md5, sha1 or sha256 digest of text (closes #94 --- fixit/cli.py | 16 ++++++++++++++++ fixit/hashing.py | 15 +++++++++++++++ tests/test_hashing.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 fixit/hashing.py create mode 100644 tests/test_hashing.py diff --git a/fixit/cli.py b/fixit/cli.py index 8a99931..998bceb 100644 --- a/fixit/cli.py +++ b/fixit/cli.py @@ -15,6 +15,7 @@ convert, health, fizzbuzz, + hashing, jsonfmt, lorem, password, @@ -164,6 +165,11 @@ def run_fizzbuzz(args: argparse.Namespace) -> None: for item in fizzbuzz.fizzbuzz(args.n): print(item) + +def run_hash(args: argparse.Namespace) -> None: + """Print the hex digest of the given text.""" + print(hashing.hash_text(args.text, args.algo)) + COMMAND_TABLE = { "wc": run_wc, "convert": run_convert, @@ -182,6 +188,7 @@ def run_fizzbuzz(args: argparse.Namespace) -> None: "coin": run_coin, "bmi": run_bmi, "fizzbuzz": run_fizzbuzz, + "hash": run_hash, } @@ -280,6 +287,15 @@ def build_parser() -> argparse.ArgumentParser: fb = sub.add_parser("fizzbuzz", help="print FizzBuzz sequence up to n") fb.add_argument("n", type=int, help="upper limit (must be at least 1)") + hs = sub.add_parser("hash", help="print the hash of a text") + hs.add_argument("text", help="text to hash") + hs.add_argument( + "--algo", + choices=hashing.SUPPORTED_ALGORITHMS, + default="sha256", + help="hash algorithm (default: sha256)", + ) + return parser diff --git a/fixit/hashing.py b/fixit/hashing.py new file mode 100644 index 0000000..bcbb657 --- /dev/null +++ b/fixit/hashing.py @@ -0,0 +1,15 @@ +"""Hash text with common algorithms.""" + +import hashlib + +SUPPORTED_ALGORITHMS = ("md5", "sha1", "sha256") + + +def hash_text(text: str, algo: str = "sha256") -> str: + """Return the hex digest of ``text`` encoded as UTF-8. + + Raises ValueError if ``algo`` is not one of SUPPORTED_ALGORITHMS. + """ + if algo not in SUPPORTED_ALGORITHMS: + raise ValueError(f"Error: unsupported algorithm '{algo}'") + return hashlib.new(algo, text.encode("utf-8")).hexdigest() diff --git a/tests/test_hashing.py b/tests/test_hashing.py new file mode 100644 index 0000000..7dfad6c --- /dev/null +++ b/tests/test_hashing.py @@ -0,0 +1,38 @@ +import hashlib + +import pytest + +from fixit.cli import main +from fixit.hashing import hash_text + + +def test_md5_of_hello(): + assert hash_text("hello", "md5") == "5d41402abc4b2a76b9719d911017c592" + + +def test_sha256_is_default(): + assert hash_text("hello") == hashlib.sha256(b"hello").hexdigest() + + +def test_empty_text_hashes_to_known_md5(): + assert hash_text("", "md5") == "d41d8cd98f00b204e9800998ecf8427e" + + +def test_unsupported_algorithm_raises(): + with pytest.raises(ValueError): + hash_text("hello", "sha512") + + +def test_hash_command_default_sha256(capsys): + assert main(["hash", "hello"]) == 0 + assert capsys.readouterr().out.strip() == hashlib.sha256(b"hello").hexdigest() + + +def test_hash_command_md5(capsys): + assert main(["hash", "hello", "--algo", "md5"]) == 0 + assert capsys.readouterr().out.strip() == "5d41402abc4b2a76b9719d911017c592" + + +def test_hash_command_rejects_unknown_algo(capsys): + with pytest.raises(SystemExit): + main(["hash", "hello", "--algo", "sha512"])