I have a set of files and a SHA256SUMS
digest file that contains a sha256()
hash for each of the files. What’s the best way to verify the integrity of my files with python?
For example, here’s how I would download the Debian 10 net installer SHA256SUMS
digest file and download/verify its the MANIFEST
file in BASH
user@host:~$ wget http://ftp.nl.debian.org/debian/dists/buster/main/installer-amd64/current/images/SHA256SUMS
--2020-08-25 02:11:20-- http://ftp.nl.debian.org/debian/dists/buster/main/installer-amd64/current/images/SHA256SUMS
Resolving ftp.nl.debian.org (ftp.nl.debian.org)... 130.89.149.21, 2001:67c:2564:a120::21
Connecting to ftp.nl.debian.org (ftp.nl.debian.org)|130.89.149.21|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 75295 (74K)
Saving to: ‘SHA256SUMS’
SHA256SUMS 100%[===================>] 73.53K 71.7KB/s in 1.0s
2020-08-25 02:11:22 (71.7 KB/s) - ‘SHA256SUMS’ saved [75295/75295]
user@host:~$ wget http://ftp.nl.debian.org/debian/dists/buster/main/installer-amd64/current/images/MANIFEST
--2020-08-25 02:11:27-- http://ftp.nl.debian.org/debian/dists/buster/main/installer-amd64/current/images/MANIFEST
Resolving ftp.nl.debian.org (ftp.nl.debian.org)... 130.89.149.21, 2001:67c:2564:a120::21
Connecting to ftp.nl.debian.org (ftp.nl.debian.org)|130.89.149.21|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1709 (1.7K)
Saving to: ‘MANIFEST’
MANIFEST 100%[===================>] 1.67K --.-KB/s in 0s
2020-08-25 02:11:28 (128 MB/s) - ‘MANIFEST’ saved [1709/1709]
user@host:~$ sha256sum --check --ignore-missing SHA256SUMS
./MANIFEST: OK
user@host:~$
What is the best way to do this same operation (download and verify the integrity of the Debian 10 MANIFEST
file using the SHA256SUMS
file) in python?
3
Answers
The following python script implements a function named
integrity_is_ok()
that takes the path to aSHA256SUMS
file and a list of files to be verified, and it returnsFalse
if any of the files couldn't be verified andTrue
otherwise.Here is an example execution:
Parts of the above code were adapted from the following answer on Ask Ubuntu:
You may calculate the sha256sums of each file as described in this blog post:
https://www.quickprogrammingtips.com/python/how-to-calculate-sha256-hash-of-a-file-in-python.html
A sample implementation to generate a new manifest file may look like:
Alternatively, this seems to be achieved by manifest-checker pip package.
You may have a look at its source here
https://github.com/TonyFlury/manifest-checkerand adjust it for python 3
Python 3.11 added
hashlib.file_digest()
https://docs.python.org/3.11/library/hashlib.html#file-hashing
Generating the digest for a file:
Compare
s
against the information you have in SHA256SUMS.