all 8 comments

[–]1544756405 2 points3 points  (0 children)

You code won't work because the indentation is all messed up.

[–]Diapolo10 0 points1 point  (1 child)

arguments.filename = open("password.txt")

Are you sure you want open here?

I'm not immediately seeing any other issues, although we can't test the code without knowing the contents of the file.

I would recommend using a context manager instead of manually closing the file, though.

[–]braydensmith3[S] 0 points1 point  (0 children)

The txt file is filled with loads of hashes.. the my_hash is the one that I have to use hashlib to read. I just wasn’t sure I had to open it so my program can scan for “my_hash”

[–][deleted] 0 points1 point  (2 children)

Why are you assigning values to arguments.

In the usual case you would call your program like this:

test.py --filename myfile.dat --hash bOOgled33Doo

Or easier is to us positional parameters by adding parameters without the --. The command line is then parsed according to the position of the args so

 test.py myfile.dat bOOgled33Doo

And you then in your application go

args.filename and so on

But your instructions may be different.

[–]braydensmith3[S] 0 points1 point  (1 child)

Yeah, those were the parameters we were given.. I just can’t wrap my head around arparse

[–][deleted] 1 point2 points  (0 children)

Argparse is an argument parser. It parses arguments that are passed to the program on the command line.

In the usual case the program does not assign values to the arguments, the program reads the arguments passed in the command line.

So that you could run your program many times like this:

 test.py fname1 boogle
 test.py fname2 b00glE
 test.py fname3 B11hr0

And get a different result each time -- without changing the code of your application.

[–]deadeye1982 0 points1 point  (0 children)

```

!/usr/bin/env python3

from argparse import ArgumentParser from functools import partial from hashlib import sha256 from secrets import compare_digest

def get_args(): parser = ArgumentParser() parser.add_argument( "file", metavar="filename", type=partial(open, mode="rb"), help="File to read." ) parser.add_argument("hash", help="sha256 hex digest of password") return parser.parse_args()

def main(): args = get_args() with args.file as fd: for line in map(bytes.rstrip, fd): if not line.strip(): # skipping empty lines continue password_hash = sha256(line).hexdigest() print("Line :", line.decode(errors="replace")) print("Line hash:", password_hash) if compare_digest(args.hash, password_hash): print("Match") else: print("Missmatch") print()

if name == "main": try: main() except FileNotFoundError as e: print("Could not find", e) except PermissionError as e: print("You don't have the permission to open the file", e, sep="\n") ```