@@ -3659,81 +3659,6 @@ def _bytes_to_int(b):
36593659 value = (value << 8) | ch
36603660 return value
36613661
3662- try:
3663- hashlib_guaranteed
3664- except NameError:
3665- hashlib_guaranteed = set(a.lower() for a in hashlib.algorithms_available)
3666-
3667- # =========================
3668- # Public checksum API
3669- # =========================
3670- def GetHeaderChecksum(inlist=None, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3671- """
3672- Serialize header fields (list/tuple => joined with delimiter + trailing delimiter;
3673- or a single field) and compute the requested checksum. Returns lowercase hex.
3674- """
3675- algo_key = (checksumtype or "md5").lower()
3676-
3677- if CheckSumSupport(algo_key, hashlib_guaranteed):
3678- h = hashlib.new(algo_key)
3679- h.update(hdr_bytes)
3680- return h.hexdigest().lower()
3681-
3682- return "0"
3683-
3684- def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3685- """
3686- Accepts bytes/str/file-like.
3687- - Hashlib algos: streamed in 1 MiB chunks.
3688- - CRC algos (crc16_ansi/ccitt/x25/kermit, crc64_iso/ecma): streamed via CRCContext for file-like.
3689- - Falls back to one-shot for non-file-like inputs.
3690- """
3691- algo_key = (checksumtype or "md5").lower()
3692-
3693- # file-like streaming
3694- if hasattr(inbytes, "read"):
3695- # hashlib
3696- if CheckSumSupport(algo_key, hashlib_guaranteed):
3697- h = hashlib.new(algo_key)
3698- while True:
3699- chunk = inbytes.read(1 << 20)
3700- if not chunk:
3701- break
3702- if not isinstance(chunk, (bytes, bytearray, memoryview)):
3703- chunk = bytes(bytearray(chunk))
3704- h.update(chunk)
3705- return h.hexdigest().lower()
3706-
3707- # not known streaming algo: fallback to one-shot bytes
3708- data = inbytes.read()
3709- if not isinstance(data, (bytes, bytearray, memoryview)):
3710- data = bytes(bytearray(data))
3711- else:
3712- data = _to_bytes(inbytes) if (encodedata or not isinstance(inbytes, (bytes, bytearray, memoryview))) else inbytes
3713- data = bytes(data)
3714-
3715- # one-shot
3716- if CheckSumSupport(algo_key, hashlib_guaranteed):
3717- h = hashlib.new(algo_key)
3718- h.update(data)
3719- return h.hexdigest().lower()
3720-
3721- return "0"
3722-
3723- def ValidateHeaderChecksum(inlist=None, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3724- calc = GetHeaderChecksum(inlist, checksumtype, True, formatspecs)
3725- want = (inchecksum or "0").strip().lower()
3726- if want.startswith("0x"):
3727- want = want[2:]
3728- return hmac.compare_digest(want, calc)
3729-
3730- def ValidateFileChecksum(infile, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3731- calc = GetFileChecksum(infile, checksumtype, True, formatspecs)
3732- want = (inchecksum or "0").strip().lower()
3733- if want.startswith("0x"):
3734- want = want[2:]
3735- return hmac.compare_digest(want, calc)
3736-
37373662# =========================
37383663# Public checksum API
37393664# =========================
@@ -3769,6 +3694,7 @@ def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__
37693694 # file-like streaming
37703695 if hasattr(inbytes, "read"):
37713696 # hashlib
3697+
37723698 if CheckSumSupport(algo_key, hashlib_guaranteed):
37733699 h = hashlib.new(algo_key)
37743700 while True:
@@ -3789,6 +3715,7 @@ def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__
37893715 data = bytes(data)
37903716
37913717 # one-shot
3718+
37923719 if CheckSumSupport(algo_key, hashlib_guaranteed):
37933720 h = hashlib.new(algo_key)
37943721 h.update(data)
@@ -3850,40 +3777,6 @@ def GetDataFromArrayAlt(structure, path, default=None):
38503777 return element
38513778
38523779
3853- def GetHeaderChecksum(inlist=[], checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3854- fileheader = AppendNullBytes(inlist, formatspecs['format_delimiter']) if isinstance(
3855- inlist, list) else AppendNullByte(inlist, formatspecs['format_delimiter'])
3856- if encodedata and hasattr(fileheader, "encode"):
3857- fileheader = fileheader.encode('UTF-8')
3858- if CheckSumSupport(checksumtype, hashlib_guaranteed):
3859- checksumoutstr = hashlib.new(checksumtype)
3860- checksumoutstr.update(fileheader)
3861- return checksumoutstr.hexdigest().lower()
3862- return format(0, 'x').lower()
3863-
3864-
3865- def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3866- if encodedata and hasattr(inbytes, "encode"):
3867- inbytes = inbytes.encode('UTF-8')
3868- if CheckSumSupport(checksumtype, hashlib_guaranteed):
3869- checksumoutstr = hashlib.new(checksumtype)
3870- checksumoutstr.update(inbytes)
3871- return checksumoutstr.hexdigest().lower()
3872- return format(0, 'x').lower()
3873-
3874-
3875- def ValidateHeaderChecksum(inlist=[], checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3876- infileheadercshex = GetHeaderChecksum(
3877- inlist, checksumtype, True, formatspecs).lower()
3878- return inchecksum.lower() == infileheadercshex
3879-
3880-
3881- def ValidateFileChecksum(infile, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3882- catinfilecshex = GetFileChecksum(
3883- infile, checksumtype, True, formatspecs).lower()
3884- return inchecksum.lower() == catinfilecshex
3885-
3886-
38873780# ========= pushback-aware delimiter reader =========
38883781class _DelimiterReader(object):
38893782 """
@@ -4305,7 +4198,7 @@ def ReadFileHeaderDataWithContent(fp, listonly=False, uncompress=True, skipcheck
43054198 fp.seek(fcsize, 1)
43064199 fcontents.seek(0, 0)
43074200 newfccs = GetFileChecksum(
4308- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4201+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
43094202 fcontents.seek(0, 0)
43104203 if(fccs != newfccs and not skipchecksum and not listonly):
43114204 VerbosePrintOut("File Content Checksum Error with file " +
@@ -4484,7 +4377,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
44844377 pyhascontents = False
44854378 fcontents.seek(0, 0)
44864379 newfccs = GetFileChecksum(
4487- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4380+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
44884381 fcontents.seek(0, 0)
44894382 if(fccs != newfccs and not skipchecksum and not listonly):
44904383 VerbosePrintOut("File Content Checksum Error with file " +
@@ -4504,7 +4397,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
45044397 cfcontents.close()
45054398 fcontents.seek(0, 0)
45064399 fccs = GetFileChecksum(
4507- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4400+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
45084401 fcontentend = fp.tell()
45094402 if(re.findall("^\\+([0-9]+)", fseeknextfile)):
45104403 fseeknextasnum = int(fseeknextfile.replace("+", ""))
@@ -4669,7 +4562,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
46694562 pyhascontents = False
46704563 fcontents.seek(0, 0)
46714564 newfccs = GetFileChecksum(
4672- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4565+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
46734566 if(fccs != newfccs and not skipchecksum and not listonly):
46744567 VerbosePrintOut("File Content Checksum Error with file " +
46754568 fname + " at offset " + str(fcontentstart))
@@ -4688,7 +4581,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
46884581 cfcontents.close()
46894582 fcontents.seek(0, 0)
46904583 fccs = GetFileChecksum(
4691- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4584+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
46924585 fcontentend = fp.tell()
46934586 if(re.findall("^\\+([0-9]+)", fseeknextfile)):
46944587 fseeknextasnum = int(fseeknextfile.replace("+", ""))
@@ -4878,7 +4771,7 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
48784771 prefcontents.write(fp.read(prefsize))
48794772 prefcontents.seek(0, 0)
48804773 prenewfccs = GetFileChecksum(
4881- prefcontents.read() , preheaderdata[-3].lower(), False, formatspecs)
4774+ prefcontents, preheaderdata[-3].lower(), False, formatspecs)
48824775 prefccs = preheaderdata[-1]
48834776 pyhascontents = True
48844777 if(prefccs != prenewfccs and not skipchecksum):
@@ -9167,12 +9060,18 @@ def CheckSumSupport(checkfor, guaranteed=True):
91679060 try:
91689061 hash_list = sorted(list(hashlib.algorithms_guaranteed))
91699062 except AttributeError:
9170- hash_list = sorted(list(hashlib.algorithms))
9063+ try:
9064+ hash_list = sorted(list(hashlib.algorithms))
9065+ except AttributeError:
9066+ hash_list = sorted(list(a.lower() for a in hashlib.algorithms_available))
91719067 else:
91729068 try:
91739069 hash_list = sorted(list(hashlib.algorithms_available))
91749070 except AttributeError:
9175- hash_list = sorted(list(hashlib.algorithms))
9071+ try:
9072+ hash_list = sorted(list(hashlib.algorithms))
9073+ except AttributeError:
9074+ hash_list = sorted(list(a.lower() for a in hashlib.algorithms_available))
91769075 checklistout = hash_list
91779076 if(checkfor in checklistout):
91789077 return True
0 commit comments