Sponsored Content
Top Forums Programming [Python] replicating "sha256 -C checksum_file.txt file.txt" Post 303037981 by da1 on Tuesday 20th of August 2019 06:39:27 AM
Old 08-20-2019
[Python] replicating "sha256 -C checksum_file.txt file.txt"

Hello everyone,

Since my python knowledge is limimted, I've challenged myself to learn as much as possible to help me with my carrere. I'm currently trying to convert a shell script to python, just to give myself a task. There is one section of the script that I'm having issues converting and that is this line:
Code:
sha256 -C SHA256 image.iso

which basically checks the checksum of "image.iso" against a textfile called SHA256, which contains the correct checksum.
A sample of "SHA256" is below (ignore the same checksum everywhere, it's just for example purposes):
Code:
SHA256 (that.iso) = 81833b79e23dc0f961ac5fb34484bca66386deb3181ddb8236870fa4f488cdd2
SHA256 (this.iso) = 81833b79e23dc0f961ac5fb34484bca66386deb3181ddb8236870fa4f488cdd2
SHA256 (image.iso) = 81833b79e23dc0f961ac5fb34484bca66386deb3181ddb8236870fa4f488cdd2

So far, I'm down to this python script:
Code:
import hashlib

workdir = '/tmp'
iso_image = 'image.iso'

def check_sha256(block_size=65536):
    sha256 = hashlib.sha256()
    with open(workdir + "/" + iso_name, 'rb') as f:
      for block in iter(lambda: f.read(block_size), b''):
        sha256.update(block)
    computed_sum = print(sha256.hexdigest())
    sha256sum

but I'm stuck as to how I should go about picking up the correct line from the "SHA256" file and then compare the hash (I guess I can do it with "if variable in" or something.

Please guide me a bit here Smilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find -name "*.txt" in Korn Shell Script

The following find command works on the Korn Shell command line: find . \( ! -name . -prune \) -type f -name "*.txt" -mtime +100 In the particular directory I'm in, the above find will list correctly the three text files that exist that haven't been modified in over 100 days: ... (3 Replies)
Discussion started by: jwperry
3 Replies

2. Shell Programming and Scripting

Sorting problem "sort -k 16,29 sample.txt > output.txt"

Hi all, Iam trying to sort the contents of the file based on the position of the file. Example: $cat sample.txt 0101020060731 ## Header record 1c1 Berger Awc ANP20070201301 4000.50 1c2 Bose W G ANP20070201609 6000.70 1c2 Andy CK ANP20070201230 28000.00... (3 Replies)
Discussion started by: ganapati
3 Replies

3. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies

4. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

5. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

6. Shell Programming and Scripting

"How to get an exact string from a txt file?"

I have many Gaussian output files, which contain a string start from "HF=" but follws the different values. I'm trying to get this exact string from these txt files. example 1, 2.524075,-0.563322,-1.285286\H,0,-2.544438,-0.678834,1.199166\H,0,2.18 ... (7 Replies)
Discussion started by: liuzhencc
7 Replies

7. Shell Programming and Scripting

"Time" command and save result in a file.txt

Hi, I'am using "time" to check execution time of some script. Is there any possibility to save time command result into a file ? (2 Replies)
Discussion started by: Physix
2 Replies

8. Shell Programming and Scripting

get rid of "^" character in a .txt file

I have a file that I ran the dos2unix utility on, it cleans up pretty well, but I noticed an occasional ^M leftover, (actual characters) these may or may not be strays from my original dos file. Either way, I want to get rid of the ^ and any character that may follow. ^M ^C or whatever. How do I... (6 Replies)
Discussion started by: ajp7701
6 Replies

9. Homework & Coursework Questions

how to change this looking for mimetype "text/plain" instead of extension *.txt?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a Shell script that looks for all text files in your home directory (including subdirectories). List... (3 Replies)
Discussion started by: rollinator
3 Replies

10. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies
SHA256SUM(1)							   User Commands						      SHA256SUM(1)

NAME
sha256sum - compute and check SHA256 message digest SYNOPSIS
sha256sum [OPTION]... [FILE]... DESCRIPTION
Print or check SHA256 (256-bit) checksums. With no FILE, or when FILE is -, read standard input. -b, --binary read in binary mode -c, --check read SHA256 sums from the FILEs and check them --tag create a BSD-style checksum -t, --text read in text mode (default) The following five options are useful only when verifying checksums: --ignore-missing don't fail or report status for missing files --quiet don't print OK for each successfully verified file --status don't output anything, status code shows success --strict exit non-zero for improperly formatted checksum lines -w, --warn warn about improperly formatted checksum lines --help display this help and exit --version output version information and exit The sums are computed as described in FIPS-180-2. When checking, the input should be a former output of this program. The default mode is to print a line with checksum, a space, a character indicating input mode ('*' for binary, ' ' for text or where binary is insignificant), and name for each FILE. AUTHOR
Written by Ulrich Drepper, Scott Miller, and David Madore. REPORTING BUGS
GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report sha256sum translation bugs to <http://translationproject.org/team/> COPYRIGHT
Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO
Full documentation at: <http://www.gnu.org/software/coreutils/sha256sum> or available locally via: info '(coreutils) sha2 utilities' GNU coreutils 8.28 January 2018 SHA256SUM(1)
All times are GMT -4. The time now is 05:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy