@@ -3660,81 +3660,6 @@ def _bytes_to_int(b):
36603660 value = (value << 8) | ch
36613661 return value
36623662
3663- try:
3664- hashlib_guaranteed
3665- except NameError:
3666- hashlib_guaranteed = set(a.lower() for a in hashlib.algorithms_available)
3667-
3668- # =========================
3669- # Public checksum API
3670- # =========================
3671- def GetHeaderChecksum(inlist=None, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3672- """
3673- Serialize header fields (list/tuple => joined with delimiter + trailing delimiter;
3674- or a single field) and compute the requested checksum. Returns lowercase hex.
3675- """
3676- algo_key = (checksumtype or "md5").lower()
3677-
3678- if CheckSumSupport(algo_key, hashlib_guaranteed):
3679- h = hashlib.new(algo_key)
3680- h.update(hdr_bytes)
3681- return h.hexdigest().lower()
3682-
3683- return "0"
3684-
3685- def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3686- """
3687- Accepts bytes/str/file-like.
3688- - Hashlib algos: streamed in 1 MiB chunks.
3689- - CRC algos (crc16_ansi/ccitt/x25/kermit, crc64_iso/ecma): streamed via CRCContext for file-like.
3690- - Falls back to one-shot for non-file-like inputs.
3691- """
3692- algo_key = (checksumtype or "md5").lower()
3693-
3694- # file-like streaming
3695- if hasattr(inbytes, "read"):
3696- # hashlib
3697- if CheckSumSupport(algo_key, hashlib_guaranteed):
3698- h = hashlib.new(algo_key)
3699- while True:
3700- chunk = inbytes.read(1 << 20)
3701- if not chunk:
3702- break
3703- if not isinstance(chunk, (bytes, bytearray, memoryview)):
3704- chunk = bytes(bytearray(chunk))
3705- h.update(chunk)
3706- return h.hexdigest().lower()
3707-
3708- # not known streaming algo: fallback to one-shot bytes
3709- data = inbytes.read()
3710- if not isinstance(data, (bytes, bytearray, memoryview)):
3711- data = bytes(bytearray(data))
3712- else:
3713- data = _to_bytes(inbytes) if (encodedata or not isinstance(inbytes, (bytes, bytearray, memoryview))) else inbytes
3714- data = bytes(data)
3715-
3716- # one-shot
3717- if CheckSumSupport(algo_key, hashlib_guaranteed):
3718- h = hashlib.new(algo_key)
3719- h.update(data)
3720- return h.hexdigest().lower()
3721-
3722- return "0"
3723-
3724- def ValidateHeaderChecksum(inlist=None, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3725- calc = GetHeaderChecksum(inlist, checksumtype, True, formatspecs)
3726- want = (inchecksum or "0").strip().lower()
3727- if want.startswith("0x"):
3728- want = want[2:]
3729- return hmac.compare_digest(want, calc)
3730-
3731- def ValidateFileChecksum(infile, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3732- calc = GetFileChecksum(infile, checksumtype, True, formatspecs)
3733- want = (inchecksum or "0").strip().lower()
3734- if want.startswith("0x"):
3735- want = want[2:]
3736- return hmac.compare_digest(want, calc)
3737-
37383663# =========================
37393664# Public checksum API
37403665# =========================
@@ -3770,6 +3695,7 @@ def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__
37703695 # file-like streaming
37713696 if hasattr(inbytes, "read"):
37723697 # hashlib
3698+
37733699 if CheckSumSupport(algo_key, hashlib_guaranteed):
37743700 h = hashlib.new(algo_key)
37753701 while True:
@@ -3790,6 +3716,7 @@ def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__
37903716 data = bytes(data)
37913717
37923718 # one-shot
3719+
37933720 if CheckSumSupport(algo_key, hashlib_guaranteed):
37943721 h = hashlib.new(algo_key)
37953722 h.update(data)
@@ -3851,40 +3778,6 @@ def GetDataFromArrayAlt(structure, path, default=None):
38513778 return element
38523779
38533780
3854- def GetHeaderChecksum(inlist=[], checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3855- fileheader = AppendNullBytes(inlist, formatspecs['format_delimiter']) if isinstance(
3856- inlist, list) else AppendNullByte(inlist, formatspecs['format_delimiter'])
3857- if encodedata and hasattr(fileheader, "encode"):
3858- fileheader = fileheader.encode('UTF-8')
3859- if CheckSumSupport(checksumtype, hashlib_guaranteed):
3860- checksumoutstr = hashlib.new(checksumtype)
3861- checksumoutstr.update(fileheader)
3862- return checksumoutstr.hexdigest().lower()
3863- return format(0, 'x').lower()
3864-
3865-
3866- def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3867- if encodedata and hasattr(inbytes, "encode"):
3868- inbytes = inbytes.encode('UTF-8')
3869- if CheckSumSupport(checksumtype, hashlib_guaranteed):
3870- checksumoutstr = hashlib.new(checksumtype)
3871- checksumoutstr.update(inbytes)
3872- return checksumoutstr.hexdigest().lower()
3873- return format(0, 'x').lower()
3874-
3875-
3876- def ValidateHeaderChecksum(inlist=[], checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3877- infileheadercshex = GetHeaderChecksum(
3878- inlist, checksumtype, True, formatspecs).lower()
3879- return inchecksum.lower() == infileheadercshex
3880-
3881-
3882- def ValidateFileChecksum(infile, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3883- catinfilecshex = GetFileChecksum(
3884- infile, checksumtype, True, formatspecs).lower()
3885- return inchecksum.lower() == catinfilecshex
3886-
3887-
38883781# ========= pushback-aware delimiter reader =========
38893782class _DelimiterReader(object):
38903783 """
@@ -4306,7 +4199,7 @@ def ReadFileHeaderDataWithContent(fp, listonly=False, uncompress=True, skipcheck
43064199 fp.seek(fcsize, 1)
43074200 fcontents.seek(0, 0)
43084201 newfccs = GetFileChecksum(
4309- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4202+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
43104203 fcontents.seek(0, 0)
43114204 if(fccs != newfccs and not skipchecksum and not listonly):
43124205 VerbosePrintOut("File Content Checksum Error with file " +
@@ -4485,7 +4378,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
44854378 pyhascontents = False
44864379 fcontents.seek(0, 0)
44874380 newfccs = GetFileChecksum(
4488- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4381+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
44894382 fcontents.seek(0, 0)
44904383 if(fccs != newfccs and not skipchecksum and not listonly):
44914384 VerbosePrintOut("File Content Checksum Error with file " +
@@ -4505,7 +4398,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
45054398 cfcontents.close()
45064399 fcontents.seek(0, 0)
45074400 fccs = GetFileChecksum(
4508- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4401+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
45094402 fcontentend = fp.tell()
45104403 if(re.findall("^\\+([0-9]+)", fseeknextfile)):
45114404 fseeknextasnum = int(fseeknextfile.replace("+", ""))
@@ -4670,7 +4563,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
46704563 pyhascontents = False
46714564 fcontents.seek(0, 0)
46724565 newfccs = GetFileChecksum(
4673- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4566+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
46744567 if(fccs != newfccs and not skipchecksum and not listonly):
46754568 VerbosePrintOut("File Content Checksum Error with file " +
46764569 fname + " at offset " + str(fcontentstart))
@@ -4689,7 +4582,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
46894582 cfcontents.close()
46904583 fcontents.seek(0, 0)
46914584 fccs = GetFileChecksum(
4692- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4585+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
46934586 fcontentend = fp.tell()
46944587 if(re.findall("^\\+([0-9]+)", fseeknextfile)):
46954588 fseeknextasnum = int(fseeknextfile.replace("+", ""))
@@ -4879,7 +4772,7 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
48794772 prefcontents.write(fp.read(prefsize))
48804773 prefcontents.seek(0, 0)
48814774 prenewfccs = GetFileChecksum(
4882- prefcontents.read() , preheaderdata[-3].lower(), False, formatspecs)
4775+ prefcontents, preheaderdata[-3].lower(), False, formatspecs)
48834776 prefccs = preheaderdata[-1]
48844777 pyhascontents = True
48854778 if(prefccs != prenewfccs and not skipchecksum):
@@ -9168,12 +9061,18 @@ def CheckSumSupport(checkfor, guaranteed=True):
91689061 try:
91699062 hash_list = sorted(list(hashlib.algorithms_guaranteed))
91709063 except AttributeError:
9171- hash_list = sorted(list(hashlib.algorithms))
9064+ try:
9065+ hash_list = sorted(list(hashlib.algorithms))
9066+ except AttributeError:
9067+ hash_list = sorted(list(a.lower() for a in hashlib.algorithms_available))
91729068 else:
91739069 try:
91749070 hash_list = sorted(list(hashlib.algorithms_available))
91759071 except AttributeError:
9176- hash_list = sorted(list(hashlib.algorithms))
9072+ try:
9073+ hash_list = sorted(list(hashlib.algorithms))
9074+ except AttributeError:
9075+ hash_list = sorted(list(a.lower() for a in hashlib.algorithms_available))
91779076 checklistout = hash_list
91789077 if(checkfor in checklistout):
91799078 return True
0 commit comments