Python Forum
how to generate sha256 hash for each line of my txt file | using python version 3.6.4
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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")
Reply
#2
This should do it.
import hashlib

with open('fruit.txt') as f:
    for line in f:
        line = line.strip()
        print(f'{hashlib.sha256(line.encode()).hexdigest()} --> {line}')
Output:
3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b --> apple b493d48364afe44d11c0165cf470a4164d1e2609911ef998be868d46ade3de4e --> banana 0f78fcc486f5315418fbf095e71c0675ee07d318e5ac4d150050cd8e57966496 --> grape 31a2d5282683edb3a22c565f199aa96fb9ffb3107af35aad92ee1cd567cfc25d --> watermelon e8c40ad2555f8a601a4946ab395a2e840bf8a96d8e7b0a66e747c97033b716af --> stawberry
Reply
#3
If my list contain some more lines like 100 words or more.
if i want to output a line in my output.txt --for-- Which sha256 starts with the prefix 'e8'
So its output the raw text & sha256 in output.txt
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  run part of a script with a different version of Python jdog 2 394 Jan-09-2024, 08:49 PM
Last Post: jdog
  How to find out from outside Python (in Windows) the current version of Python? pstein 4 679 Oct-04-2023, 10:01 AM
Last Post: snippsat
  Help with Python Script to generate SVG Dot Matrix Pattern for LED Light Guide iamrickm 2 708 Aug-25-2023, 06:07 PM
Last Post: iamrickm
  How to resolve version conflicts in Python? taeefnajib 0 873 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  [SOLVED] How to crack hash with hashlib Milan 0 1,325 Mar-09-2023, 08:25 PM
Last Post: Milan
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,391 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Python venv and PIP version issue JanOlvegg 2 1,199 Feb-22-2023, 02:22 AM
Last Post: JanOlvegg
  Python Version upgrade nitinkukreja 1 852 Feb-04-2023, 10:27 PM
Last Post: Larz60+
  Getting last line of each line occurrence in a file tester_V 1 811 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020