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
sha256(n)						  SHA-x Message-Digest Algorithm						 sha256(n)

__________________________________________________________________________________________________________________________________________________

NAME
sha256 - SHA256 Message-Digest Algorithm SYNOPSIS
package require Tcl 8.2 package require sha256 ?1.0.2? ::sha2::sha256 ?-hex|-bin? [ -channel channel | -file filename | string ] ::sha2::sha224 ?-hex|-bin? [ -channel channel | -file filename | string ] ::sha2::hmac key string ::sha2::hmac ?-hex|-bin? -key key [ -channel channel | -file filename | string ] ::sha2::SHA256Init ::sha2::SHA224Init ::sha2::SHA256Update token data ::sha2::SHA256Final token ::sha2::SHA224Final token ::sha2::HMACInit key ::sha2::HMACUpdate token data ::sha2::HMACFinal token _________________________________________________________________ DESCRIPTION
This package provides an implementation in Tcl of the SHA256 and SHA224 message-digest algorithms as specified by FIPS PUB 180-1 (1). These algorithms take a message and generates a 256-bit (224-bit) digest from the input. The SHA2 algorithms are related to the SHA1 algorithm. This package also includes support for creating keyed message-digests using the HMAC algorithm from RFC 2104 (3) with SHA256 as the mes- sage-digest. COMMANDS
::sha2::sha256 ?-hex|-bin? [ -channel channel | -file filename | string ] The command takes a message and returns the SHA256 digest of this message as a hexadecimal string. You may request the result as binary data by giving -bin. The data to be hashed can be specified either as a string argument to the sha256 command, or as a filename or a pre-opened channel. If the -filename argument is given then the file is opened, the data read and hashed and the file is closed. If the -channel argu- ment is given then data is read from the channel until the end of file. The channel is not closed. NOTE use of the channel or file- name options results in the internal use of vwait. To avoid nested event loops in Tk or tclhttpd applications you should use the incremental programming API (see below). Only one of -file, -channel or string should be given. ::sha2::sha224 ?-hex|-bin? [ -channel channel | -file filename | string ] Like ::sha2::sha256, except that the SHA224 digest is returned. ::sha2::hmac key string ::sha2::hmac ?-hex|-bin? -key key [ -channel channel | -file filename | string ] Calculate an Hashed Message Authentication digest (HMAC) using the SHA256 digest algorithm. HMACs are described in RFC 2104 (3) and provide an SHA256 digest that includes a key. All options other than -key are as for the ::sha2::sha256 command. PROGRAMMING INTERFACE
For the programmer, the SHA256 hash can be viewed as a bucket into which one pours data. When you have finished, you extract a value that is derived from the data that was poured into the bucket. The programming interface to the SHA256 hash operates on a token (equivalent to the bucket). You call SHA256Init to obtain a token and then call SHA256Update as many times as required to add data to the hash. To release any resources and obtain the hash value, you then call SHA256Final. An equivalent set of functions gives you a keyed digest (HMAC). If you have critcl and have built the tcllibc package then the implementation of the hashing function will be performed by compiled code. Failing that there is a pure-tcl equivalent. The programming interface remains the same in all cases. ::sha2::SHA256Init ::sha2::SHA224Init Begins a new SHA256/SHA224 hash. Returns a token ID that must be used for the remaining functions. ::sha2::SHA256Update token data Add data to the hash identified by token. Calling SHA256Update $token "abcd" is equivalent to calling SHA256Update $token "ab" fol- lowed by SHA256Update $token "cb". See EXAMPLES. Note that this command is used for both SHA256 and SHA224. Only the initialization and finalization commands of both hashes differ. ::sha2::SHA256Final token ::sha2::SHA224Final token Returns the hash value and releases any resources held by this token. Once this command completes the token will be invalid. The result is a binary string of 32/28 bytes representing the 256/224 bit SHA256 / SHA224 digest value. ::sha2::HMACInit key This is equivalent to the ::sha2::SHA256Init command except that it requires the key that will be included in the HMAC. ::sha2::HMACUpdate token data ::sha2::HMACFinal token These commands are identical to the SHA256 equivalent commands. EXAMPLES
% sha2::sha256 "Tcl does SHA256" 0b91043ee484abd83c3e4b08d6034d71b937026379f0f59bda6e625e6e214789 % sha2::hmac Sekret "Tcl does SHA256" 4f9352c64d655e8a36abe73e6163a9d7a54039877c1c92ec90b07d48d4e854e0 % set tok [sha2::SHA256Init] ::sha2::1 % sha2::SHA256Update $tok "Tcl " % sha2::SHA256Update $tok "does " % sha2::SHA256Update $tok "SHA256" % sha2::Hex [sha2::SHA256Final $tok] 0b91043ee484abd83c3e4b08d6034d71b937026379f0f59bda6e625e6e214789 REFERENCES
[1] "Secure Hash Standard", National Institute of Standards and Technology, U.S. Department Of Commerce, April 1995. (http://www.itl.nist.gov/fipspubs/fip180-1.htm) [2] Rivest, R., "The MD4 Message Digest Algorithm", RFC 1320, MIT, April 1992. (http://www.rfc-editor.org/rfc/rfc1320.txt) [3] Krawczyk, H., Bellare, M. and Canetti, R. "HMAC: Keyed-Hashing for Message Authentication", RFC 2104, February 1997. (http://www.rfc-editor.org/rfc/rfc2104.txt) BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category sha1 of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. SEE ALSO
md4, md5, ripemd128, ripemd160, sha1 KEYWORDS
FIPS 180-1, hashing, message-digest, rfc 2104, security, sha256 COPYRIGHT
Copyright (c) 2008, Andreas Kupries <andreas_kupries@users.sourceforge.net> sha1 1.0.2 sha256(n)
All times are GMT -4. The time now is 06:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy