how to generate sha256 hash for each line of my txt file | using python version 3.6.4


#1

I am creating a project for generating sha256 hash from text file
I want to read each line from my text file and generate sha256 for it.

Here is the code i written but it is not correct. please see and suggest me.
my text file contains:
apple
banana
grape
watermelon
stawberry

my Code is :


import hashlib
f = open("D:/12/1.txt",'r')
x = f.readline()
while(x !=""):
    hash_object = hashlib.sha256(b'x')
    hex_dig = hash_object.hexdigest()
    print(x.strip(),"",hex_dig)
    x= f.readline()
 
input("Successful..Press enter to exit")

#2

you never close the file, and you can’t trust python to do it.

use with open() as to make things easier on yourself.

i am unsure why you would use readline and while loop, you can just use a for loop to loop over all the lines.

strings needs to be encoded, so its much easier to use bytes, which we can do with open(), and then we can verify encoding much easier.


#3

It’s fine if the program terminates immediately (which this one doesn’t though, since it waits for input)


#4

I got the code working…now I want to ask if I have a large file so Now I want to output raw text and sha256 —for— which sha256 has a prefix of ‘e1’ .so it write the line raw text and sha256 in output.txt or output.csv and if it finds another it writes in same output file in next line.


#5

There is a line, helping is fine.

But don’t expect me to code what you just asked, you will have to do the coding, and we can help, that is fine. But the longer the help takes, the more you should attempt on your own, which is will reflect in my answers