added tarsum

added simple python tar-file-contents-hasher
This commit is contained in:
Tobias Kessels
2018-12-18 11:39:05 +01:00
parent 618a87479d
commit f763dddca7

33
tarsum.py Executable file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/python3 -u
import tarfile
import sys
import hashlib
#ask for parameters
#1 = hashtype or md5 by defualt
#2 = filename of tarfile
try:
if len(sys.argv) == 3:
hashtype=sys.argv[1]
tarfilename=sys.argv[2]
else:
hashtype="md5"
tarfilename=sys.argv[1]
h=hashlib.new(hashtype)
tf=tarfile.open(tarfilename,'r')
#print usage if anything goes wrong
except Exception as e :
print(e)
print("usage: tarsum.py [hashtype] tarfile.tgz")
print("hashtype can be:")
print(hashlib.algorithms_available)
print("md5 is default")
exit(1)
for file in tf:
if file.isfile():
h=hashlib.new(hashtype)
extracted_file=tf.extractfile(file)
for chunk in iter(lambda: extracted_file.read(h.block_size),b''):
h.update(chunk)
print("{1} {0}".format(file.name,h.hexdigest()))