@@ -3652,81 +3652,6 @@ def _bytes_to_int(b):
36523652 value = (value << 8) | ch
36533653 return value
36543654
3655- try:
3656- hashlib_guaranteed
3657- except NameError:
3658- hashlib_guaranteed = set(a.lower() for a in hashlib.algorithms_available)
3659-
3660- # =========================
3661- # Public checksum API
3662- # =========================
3663- def GetHeaderChecksum(inlist=None, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3664- """
3665- Serialize header fields (list/tuple => joined with delimiter + trailing delimiter;
3666- or a single field) and compute the requested checksum. Returns lowercase hex.
3667- """
3668- algo_key = (checksumtype or "md5").lower()
3669-
3670- if CheckSumSupport(algo_key, hashlib_guaranteed):
3671- h = hashlib.new(algo_key)
3672- h.update(hdr_bytes)
3673- return h.hexdigest().lower()
3674-
3675- return "0"
3676-
3677- def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3678- """
3679- Accepts bytes/str/file-like.
3680- - Hashlib algos: streamed in 1 MiB chunks.
3681- - CRC algos (crc16_ansi/ccitt/x25/kermit, crc64_iso/ecma): streamed via CRCContext for file-like.
3682- - Falls back to one-shot for non-file-like inputs.
3683- """
3684- algo_key = (checksumtype or "md5").lower()
3685-
3686- # file-like streaming
3687- if hasattr(inbytes, "read"):
3688- # hashlib
3689- if CheckSumSupport(algo_key, hashlib_guaranteed):
3690- h = hashlib.new(algo_key)
3691- while True:
3692- chunk = inbytes.read(1 << 20)
3693- if not chunk:
3694- break
3695- if not isinstance(chunk, (bytes, bytearray, memoryview)):
3696- chunk = bytes(bytearray(chunk))
3697- h.update(chunk)
3698- return h.hexdigest().lower()
3699-
3700- # not known streaming algo: fallback to one-shot bytes
3701- data = inbytes.read()
3702- if not isinstance(data, (bytes, bytearray, memoryview)):
3703- data = bytes(bytearray(data))
3704- else:
3705- data = _to_bytes(inbytes) if (encodedata or not isinstance(inbytes, (bytes, bytearray, memoryview))) else inbytes
3706- data = bytes(data)
3707-
3708- # one-shot
3709- if CheckSumSupport(algo_key, hashlib_guaranteed):
3710- h = hashlib.new(algo_key)
3711- h.update(data)
3712- return h.hexdigest().lower()
3713-
3714- return "0"
3715-
3716- def ValidateHeaderChecksum(inlist=None, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3717- calc = GetHeaderChecksum(inlist, checksumtype, True, formatspecs)
3718- want = (inchecksum or "0").strip().lower()
3719- if want.startswith("0x"):
3720- want = want[2:]
3721- return hmac.compare_digest(want, calc)
3722-
3723- def ValidateFileChecksum(infile, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3724- calc = GetFileChecksum(infile, 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-
37303655# =========================
37313656# Public checksum API
37323657# =========================
@@ -3762,6 +3687,7 @@ def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__
37623687 # file-like streaming
37633688 if hasattr(inbytes, "read"):
37643689 # hashlib
3690+
37653691 if CheckSumSupport(algo_key, hashlib_guaranteed):
37663692 h = hashlib.new(algo_key)
37673693 while True:
@@ -3782,6 +3708,7 @@ def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__
37823708 data = bytes(data)
37833709
37843710 # one-shot
3711+
37853712 if CheckSumSupport(algo_key, hashlib_guaranteed):
37863713 h = hashlib.new(algo_key)
37873714 h.update(data)
@@ -3843,40 +3770,6 @@ def GetDataFromArrayAlt(structure, path, default=None):
38433770 return element
38443771
38453772
3846- def GetHeaderChecksum(inlist=[], checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3847- fileheader = AppendNullBytes(inlist, formatspecs['format_delimiter']) if isinstance(
3848- inlist, list) else AppendNullByte(inlist, formatspecs['format_delimiter'])
3849- if encodedata and hasattr(fileheader, "encode"):
3850- fileheader = fileheader.encode('UTF-8')
3851- if CheckSumSupport(checksumtype, hashlib_guaranteed):
3852- checksumoutstr = hashlib.new(checksumtype)
3853- checksumoutstr.update(fileheader)
3854- return checksumoutstr.hexdigest().lower()
3855- return format(0, 'x').lower()
3856-
3857-
3858- def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3859- if encodedata and hasattr(inbytes, "encode"):
3860- inbytes = inbytes.encode('UTF-8')
3861- if CheckSumSupport(checksumtype, hashlib_guaranteed):
3862- checksumoutstr = hashlib.new(checksumtype)
3863- checksumoutstr.update(inbytes)
3864- return checksumoutstr.hexdigest().lower()
3865- return format(0, 'x').lower()
3866-
3867-
3868- def ValidateHeaderChecksum(inlist=[], checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3869- infileheadercshex = GetHeaderChecksum(
3870- inlist, checksumtype, True, formatspecs).lower()
3871- return inchecksum.lower() == infileheadercshex
3872-
3873-
3874- def ValidateFileChecksum(infile, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__):
3875- catinfilecshex = GetFileChecksum(
3876- infile, checksumtype, True, formatspecs).lower()
3877- return inchecksum.lower() == catinfilecshex
3878-
3879-
38803773# ========= pushback-aware delimiter reader =========
38813774class _DelimiterReader(object):
38823775 """
@@ -4298,7 +4191,7 @@ def ReadFileHeaderDataWithContent(fp, listonly=False, uncompress=True, skipcheck
42984191 fp.seek(fcsize, 1)
42994192 fcontents.seek(0, 0)
43004193 newfccs = GetFileChecksum(
4301- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4194+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
43024195 fcontents.seek(0, 0)
43034196 if(fccs != newfccs and not skipchecksum and not listonly):
43044197 VerbosePrintOut("File Content Checksum Error with file " +
@@ -4477,7 +4370,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
44774370 pyhascontents = False
44784371 fcontents.seek(0, 0)
44794372 newfccs = GetFileChecksum(
4480- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4373+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
44814374 fcontents.seek(0, 0)
44824375 if(fccs != newfccs and not skipchecksum and not listonly):
44834376 VerbosePrintOut("File Content Checksum Error with file " +
@@ -4497,7 +4390,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
44974390 cfcontents.close()
44984391 fcontents.seek(0, 0)
44994392 fccs = GetFileChecksum(
4500- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4393+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
45014394 fcontentend = fp.tell()
45024395 if(re.findall("^\\+([0-9]+)", fseeknextfile)):
45034396 fseeknextasnum = int(fseeknextfile.replace("+", ""))
@@ -4662,7 +4555,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
46624555 pyhascontents = False
46634556 fcontents.seek(0, 0)
46644557 newfccs = GetFileChecksum(
4665- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4558+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
46664559 if(fccs != newfccs and not skipchecksum and not listonly):
46674560 VerbosePrintOut("File Content Checksum Error with file " +
46684561 fname + " at offset " + str(fcontentstart))
@@ -4681,7 +4574,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
46814574 cfcontents.close()
46824575 fcontents.seek(0, 0)
46834576 fccs = GetFileChecksum(
4684- fcontents.read() , HeaderOut[-3].lower(), False, formatspecs)
4577+ fcontents, HeaderOut[-3].lower(), False, formatspecs)
46854578 fcontentend = fp.tell()
46864579 if(re.findall("^\\+([0-9]+)", fseeknextfile)):
46874580 fseeknextasnum = int(fseeknextfile.replace("+", ""))
@@ -4871,7 +4764,7 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
48714764 prefcontents.write(fp.read(prefsize))
48724765 prefcontents.seek(0, 0)
48734766 prenewfccs = GetFileChecksum(
4874- prefcontents.read() , preheaderdata[-3].lower(), False, formatspecs)
4767+ prefcontents, preheaderdata[-3].lower(), False, formatspecs)
48754768 prefccs = preheaderdata[-1]
48764769 pyhascontents = True
48774770 if(prefccs != prenewfccs and not skipchecksum):
@@ -9160,12 +9053,18 @@ def CheckSumSupport(checkfor, guaranteed=True):
91609053 try:
91619054 hash_list = sorted(list(hashlib.algorithms_guaranteed))
91629055 except AttributeError:
9163- hash_list = sorted(list(hashlib.algorithms))
9056+ try:
9057+ hash_list = sorted(list(hashlib.algorithms))
9058+ except AttributeError:
9059+ hash_list = sorted(list(a.lower() for a in hashlib.algorithms_available))
91649060 else:
91659061 try:
91669062 hash_list = sorted(list(hashlib.algorithms_available))
91679063 except AttributeError:
9168- 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))
91699068 checklistout = hash_list
91709069 if(checkfor in checklistout):
91719070 return True
0 commit comments