Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try decompression methods when file checksum fails #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 41 additions & 18 deletions apt_offline_core/AptOfflineLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,52 @@ class Checksum:
def HashMessageDigestAlgorithms( self, checksum, HashType, checksumFile ):

try:
data = open( checksumFile, 'rb' )
if HashType == "sha256":
Hash = self.sha256( data )
elif HashType == "md5" or HashType == "md5sum":
Hash = self.md5( data )
else: Hash = None
except IOError:
data = open(checksumFile, 'rb')
data_bytes = data.read()
Hash = self.find_hash(data_bytes, HashType)
except IOError as e:
print(e)
return False
data.close()

if Hash == checksum:
return True

# Hash doesn't match; try decompressing archives
print("Trying to decompress with lzma...")
try:
decompressor = lzma.LZMADecompressor()
data_decomp = decompressor.decompress(data_bytes)
hash_decomp = self.find_hash(data_decomp, HashType)

if hash_decomp == checksum:
return True
except Exception as e:
pass

print("Trying to decompress with bz2...")
try:
decompressor = bz2.BZ2Decompressor()
data_decomp = decompressor.decompress(data_bytes)
hash_decomp = self.find_hash(data_decomp, HashType)

if hash_decomp == checksum:
return True
except Exception as e:
pass

return False

def sha256( self, data ):
sha256 = hashlib.sha256()
sha256.update( data.read() )
return sha256.hexdigest()

def md5( self, data ):
md5hash = hashlib.md5()
md5hash.update( data.read() )
return md5hash.hexdigest()

def find_hash(self, data_bytes, hash_type):
if hash_type == "sha256":
ht = "sha256"
elif hash_type in {"md5", "md5sum"}:
ht = "md5"
else:
return None
h = hashlib.new(ht)
h.update(data_bytes)
return h.hexdigest()

def CheckHashDigest( self, checksumFile, checksum ):
'''Return Bool against file and its checksum'''
Expand Down