Home > Software engineering >  Cannot hash every line on of a whole file on Python using VSC
Cannot hash every line on of a whole file on Python using VSC

Time:04-16

I'm using Python on VSC on a project, it requires me to hash the whole file (46KB with 5579 lines). However, on VSC, it only shows the hash table of the last 1012 lines. I didn't know what was happening and I could not fix it. This is my code:

import hashlib
def SHA1_hash(string):
    hash_obj = hashlib.sha1(string.encode())
    return(hash_obj.hexdigest())

with open("Project file/dict.txt") as f: 
    for line in f.readlines():
        print(SHA1_hash(line))

Picture of my text file starting from "writings":

The output should starts with:

5AD930D43A7851DC6649558BA6BEDD44F14E737C
(hash SHA-1 of "writings")

However, it is like this:

Picture of output terminal, the output started with a different hash string:

Why is this happening? Why am I getting the wrong hash as output?

CodePudding user response:

Because of the \n after every line(except last line), I can not get 5AD930D43A7851DC6649558BA6BEDD44F14E737C of writings.

Take print(SHA1_hash(line.strip())) to replace print(SHA1_hash(line)) can solve the problem.

But I must admit I can not reproduce your outputs, could you check the string in the dict.txt file?

  • Related