2019-12-17 03:40:08 -05:00
|
|
|
import copy
|
2019-12-11 06:33:24 -05:00
|
|
|
import hashlib
|
2019-12-17 03:40:08 -05:00
|
|
|
import os
|
2019-12-11 06:33:24 -05:00
|
|
|
import pathlib
|
|
|
|
import zlib
|
|
|
|
##
|
|
|
|
import aif.constants_fallback
|
|
|
|
|
|
|
|
|
|
|
|
class Hash(object):
|
2019-12-17 03:40:08 -05:00
|
|
|
def __init__(self, hash_algos = None, *args, **kwargs):
|
2019-12-11 06:33:24 -05:00
|
|
|
self.hashers = None
|
2019-12-17 03:40:08 -05:00
|
|
|
self.valid_hashtypes = list(aif.constants_fallback.HASH_SUPPORTED_TYPES)
|
|
|
|
self.hash_algos = hash_algos
|
|
|
|
self.configure()
|
2019-12-11 06:33:24 -05:00
|
|
|
|
2019-12-17 03:40:08 -05:00
|
|
|
def configure(self, *args, **kwargs):
|
2019-12-11 06:33:24 -05:00
|
|
|
self.hashers = {}
|
2019-12-17 03:40:08 -05:00
|
|
|
if self.hash_algos:
|
|
|
|
if not isinstance(self.hash_algos, list):
|
|
|
|
self.hash_algos = [self.hash_algos]
|
2019-12-11 06:33:24 -05:00
|
|
|
else:
|
2019-12-17 03:40:08 -05:00
|
|
|
self.hash_algos = copy.deepcopy(self.valid_hashtypes)
|
|
|
|
for h in self.hash_algos:
|
|
|
|
if h not in self.valid_hashtypes:
|
2019-12-11 06:33:24 -05:00
|
|
|
raise ValueError('Hash algorithm not supported')
|
|
|
|
if h not in aif.constants_fallback.HASH_EXTRA_SUPPORTED_TYPES:
|
|
|
|
hasher = hashlib.new(h)
|
|
|
|
else: # adler32 and crc32
|
|
|
|
hasher = getattr(zlib, h)
|
|
|
|
self.hashers[h] = hasher
|
|
|
|
return()
|
|
|
|
|
2019-12-17 03:40:08 -05:00
|
|
|
def hashData(self, data, *args, **kwargs):
|
2019-12-11 06:33:24 -05:00
|
|
|
results = {}
|
2019-12-17 03:40:08 -05:00
|
|
|
if not self.hashers or not self.hash_algos:
|
2019-12-11 06:33:24 -05:00
|
|
|
self.configure()
|
|
|
|
for hashtype, hasher in self.hashers.items():
|
|
|
|
if hashtype in aif.constants_fallback.HASH_EXTRA_SUPPORTED_TYPES:
|
|
|
|
results[hashtype] = hasher(data)
|
|
|
|
else:
|
2019-12-17 03:40:08 -05:00
|
|
|
hasher.update(data)
|
|
|
|
results[hashtype] = hasher.hexdigest()
|
2019-12-11 06:33:24 -05:00
|
|
|
return(results)
|
|
|
|
|
2019-12-17 03:40:08 -05:00
|
|
|
def hashFile(self, file_path, *args, **kwargs):
|
|
|
|
if not isinstance(file_path, (str, pathlib.Path, pathlib.PurePath)):
|
2019-12-11 06:33:24 -05:00
|
|
|
raise ValueError('file_path must be a path expression')
|
|
|
|
file_path = str(file_path)
|
|
|
|
with open(file_path, 'rb') as fh:
|
|
|
|
results = self.hashData(fh.read())
|
|
|
|
return(results)
|
2019-12-17 03:40:08 -05:00
|
|
|
|
|
|
|
def verifyData(self, data, checksum, checksum_type, *args, **kwargs):
|
|
|
|
if isinstance(data, str):
|
|
|
|
data = data.encode('utf-8')
|
|
|
|
if not isinstance(checksum, str):
|
|
|
|
checksum = checksum.decode('utf-8')
|
|
|
|
if checksum_type not in self.hash_algos:
|
|
|
|
raise ValueError('Hash algorithm not supported; try reconfiguring')
|
|
|
|
self.configure()
|
|
|
|
cksum = self.hashData(data)
|
|
|
|
cksum_htype = cksum[checksum_type]
|
|
|
|
if cksum == checksum:
|
|
|
|
result = True
|
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
return(result)
|
|
|
|
|
|
|
|
def verifyFile(self, filepath, checksum, checksum_type, *args, **kwargs):
|
|
|
|
filepath = os.path.abspath(os.path.expanduser(filepath))
|
|
|
|
with open(filepath, 'rb') as fh:
|
|
|
|
result = self.verifyData(fh.read(), checksum, checksum_type, **kwargs)
|
|
|
|
return(result)
|