Last active
April 8, 2020 16:38
-
-
Save DaveCTurner/8765561 to your computer and use it in GitHub Desktop.
SHA1 hashes of contents of TAR archive without extracting the archive first
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import tarfile | |
import hashlib | |
for filename in sys.argv[1:]: | |
print filename | |
with tarfile.open(filename, 'r') as tar: | |
for tarinfo in tar: | |
if tarinfo.isreg(): | |
flo = tar.extractfile(tarinfo) # NB doesn't really extract the file, just gives you a stream (file-like-object) for reading it | |
hash = hashlib.sha1() | |
while True: | |
data = flo.read(2**20) | |
if not data: | |
break | |
hash.update(data) | |
flo.close() | |
print hash.hexdigest(), tarinfo.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment