0
import hashlib

with open("common.txt", "r") as f:
    f = f.readlines()
print(f)
with open("hashes.txt", "r") as e:
    e = e.readlines()

print(e)
print("\n")

print(e)

print("\n")

for password in f:
    h = hashlib.new("SHA256")
    h.update(password.encode())
    password = h.hexdigest()
    print(h.hexdigest())
   
for line in f,e:
    if e == f:
       print("hash found")
    else:
        print("error")

I am not sure how to check if a hash is found between the two text files I am comparing. I used this approach but that did not work.

  • I see you have posted some code, but I'm not sure what it does. The first part seems to read the contents of two files and print them out. However, I'm not sure what the next two loops do. The first seems to print some hashes and the second seems to compare the two files. Can you describe what you are trying to accomplish here? – quamrana Jul 31 '23 at 19:32
  • It creates a hash and then assigns it to plain text common passwords then try's to compare it with some of the hashed versions. – Skrug Nuggsta Jul 31 '23 at 19:35
  • Does this answer your question? [How do I iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-do-i-iterate-through-two-lists-in-parallel) – mkrieger1 Jul 31 '23 at 19:41
  • You meant `for f_line, e_line in zip(f, e): if e_line == f_line: ...` – mkrieger1 Jul 31 '23 at 19:41
  • 1
    I see that you create a variable `password`, but then you don't use that elsewhere and certainly not in any way related to the contents of `"hashes.txt"`. – quamrana Jul 31 '23 at 19:41

1 Answers1

0

Assuming both files have exactly the same line count and valid values on each line, it's possible to simplify your script a bit. Something like this might work:

# import the desired hashing function
from hashlib import sha256

# open both files in a single syntactic context
with open("common.txt", "rb") as f1, open("hashes.txt", "rb") as f2:
    
    # for each line in the passwords file
    for password in f1:
        
        # evaluate the password hash
        hash_obj = sha256(password)
        hashed_pasword = hash_obj.hex_digest()
        
        # get the corresponding hash from the other file
        password_hash = f2.readline()
        
        # print the comparison
        print(
            "hashed pw:", hashed_password,
            "pw hash:", password_hash ,
            "equal:" hashed_password == password_hash
        )

Some other debugging steps include making sure that using sha256.hex_digest rather than sha256.digest is correct.

It's hard to know without sample output. Hope this helps!

thebadgateway
  • 433
  • 1
  • 4
  • 7