Skip to content

Commit 98deb2f

Browse files
authored
Add files via upload
1 parent 0a570cf commit 98deb2f

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

pyarchivefile.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3816,15 +3816,28 @@ def ValidateHeaderChecksum(inlist=None, checksumtype="md5", inchecksum="0", form
38163816
want = (inchecksum or "0").strip().lower()
38173817
if want.startswith("0x"):
38183818
want = want[2:]
3819-
return hmac.compare_digest(want, calc)
3819+
return CheckChecksums(want, calc)
38203820

38213821
def ValidateFileChecksum(infile, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__, saltkey=None):
38223822
calc = GetFileChecksum(infile, checksumtype, True, formatspecs, saltkey)
38233823
want = (inchecksum or "0").strip().lower()
38243824
if want.startswith("0x"):
38253825
want = want[2:]
3826-
return hmac.compare_digest(want, calc)
3826+
return CheckChecksums(want, calc)
38273827

3828+
def CheckChecksums(inchecksum, outchecksum):
3829+
# Normalize as text first
3830+
calc = (inchecksum or "0").strip().lower()
3831+
want = (outchecksum or "0").strip().lower()
3832+
3833+
if want.startswith("0x"):
3834+
want = want[2:]
3835+
3836+
# Now force both to bytes
3837+
calc_b = _to_bytes(calc) # defaults to utf-8, strict
3838+
want_b = _to_bytes(want)
3839+
3840+
return hmac.compare_digest(want_b, calc_b)
38283841

38293842
def MajorMinorToDev(major, minor):
38303843
"""
@@ -4286,7 +4299,7 @@ def ReadFileHeaderDataWithContent(fp, listonly=False, uncompress=True, skipcheck
42864299
pass
42874300
fp.seek(len(delimiter), 1)
42884301
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
4289-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
4302+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
42904303
VerbosePrintOut("File JSON Data Checksum Error with file " +
42914304
fname + " at offset " + str(fheaderstart))
42924305
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -4315,7 +4328,7 @@ def ReadFileHeaderDataWithContent(fp, listonly=False, uncompress=True, skipcheck
43154328
newfccs = GetFileChecksum(
43164329
fcontents, HeaderOut[-3].lower(), False, formatspecs)
43174330
fcontents.seek(0, 0)
4318-
if(not hmac.compare_digest(fccs, newfccs) and not skipchecksum and not listonly):
4331+
if(not CheckChecksums(fccs, newfccs) and not skipchecksum and not listonly):
43194332
VerbosePrintOut("File Content Checksum Error with file " +
43204333
fname + " at offset " + str(fcontentstart))
43214334
VerbosePrintOut("'" + fccs + "' != " + "'" + newfccs + "'")
@@ -4498,7 +4511,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
44984511
fp.seek(len(delimiter), 1)
44994512
fjend = fp.tell() - 1
45004513
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
4501-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
4514+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
45024515
VerbosePrintOut("File JSON Data Checksum Error with file " +
45034516
fname + " at offset " + str(fheaderstart))
45044517
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -4532,7 +4545,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
45324545
newfccs = GetFileChecksum(
45334546
fcontents, HeaderOut[-3].lower(), False, formatspecs)
45344547
fcontents.seek(0, 0)
4535-
if(not hmac.compare_digest(fccs, newfccs) and not skipchecksum and not listonly):
4548+
if(not CheckChecksums(fccs, newfccs) and not skipchecksum and not listonly):
45364549
VerbosePrintOut("File Content Checksum Error with file " +
45374550
fname + " at offset " + str(fcontentstart))
45384551
VerbosePrintOut("'" + fccs + "' != " + "'" + newfccs + "'")
@@ -4712,7 +4725,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
47124725
pass
47134726
fp.seek(len(delimiter), 1)
47144727
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
4715-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
4728+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
47164729
VerbosePrintOut("File JSON Data Checksum Error with file " +
47174730
fname + " at offset " + str(fheaderstart))
47184731
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -4745,7 +4758,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
47454758
fcontents.seek(0, 0)
47464759
newfccs = GetFileChecksum(
47474760
fcontents, HeaderOut[-3].lower(), False, formatspecs)
4748-
if(not hmac.compare_digest(fccs, newfccs) and not skipchecksum and not listonly):
4761+
if(not CheckChecksums(fccs, newfccs) and not skipchecksum and not listonly):
47494762
VerbosePrintOut("File Content Checksum Error with file " +
47504763
fname + " at offset " + str(fcontentstart))
47514764
VerbosePrintOut("'" + fccs + "' != " + "'" + newfccs + "'")
@@ -5005,7 +5018,7 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
50055018
else:
50065019
return False
50075020
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
5008-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
5021+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
50095022
VerbosePrintOut("File JSON Data Checksum Error with file " +
50105023
fname + " at offset " + str(fheaderstart))
50115024
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -5050,15 +5063,15 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
50505063
prejsoncontent = fp.read(prefjsonsize).decode("UTF-8")
50515064
fp.seek(len(delimiter), 1)
50525065
prejsonfcs = GetFileChecksum(prejsoncontent, prefjsonchecksumtype, True, formatspecs)
5053-
if(not hmac.compare_digest(prefjsonchecksum, prejsonfcs) and not skipchecksum):
5066+
if(not CheckChecksums(prefjsonchecksum, prejsonfcs) and not skipchecksum):
50545067
VerbosePrintOut("File JSON Data Checksum Error with file " +
50555068
prefname + " at offset " + str(prefhstart))
50565069
VerbosePrintOut("'" + prefjsonchecksum + "' != " + "'" + prejsonfcs + "'")
50575070
return False
50585071
prenewfcs = GetHeaderChecksum(
50595072
preheaderdata[:-2], preheaderdata[-4].lower(), True, formatspecs)
50605073
prefcs = preheaderdata[-2]
5061-
if(not hmac.compare_digest(prefcs, prenewfcs) and not skipchecksum):
5074+
if(not CheckChecksums(prefcs, prenewfcs) and not skipchecksum):
50625075
VerbosePrintOut("File Header Checksum Error with file " +
50635076
prefname + " at offset " + str(prefhstart))
50645077
VerbosePrintOut("'" + prefcs + "' != " +
@@ -5077,7 +5090,7 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
50775090
prefcontents, preheaderdata[-3].lower(), False, formatspecs)
50785091
prefccs = preheaderdata[-1]
50795092
pyhascontents = True
5080-
if(not hmac.compare_digest(prefccs, prenewfccs) and not skipchecksum):
5093+
if(not CheckChecksums(prefccs, prenewfccs) and not skipchecksum):
50815094
VerbosePrintOut("File Content Checksum Error with file " +
50825095
prefname + " at offset " + str(prefcontentstart))
50835096
VerbosePrintOut("'" + prefccs +
@@ -5191,7 +5204,7 @@ def ReadFileDataWithContentToList(fp, filestart=0, seekstart=0, seekend=0, listo
51915204
else:
51925205
return False
51935206
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
5194-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
5207+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
51955208
VerbosePrintOut("File JSON Data Checksum Error with file " +
51965209
fname + " at offset " + str(fheaderstart))
51975210
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -5241,15 +5254,15 @@ def ReadFileDataWithContentToList(fp, filestart=0, seekstart=0, seekend=0, listo
52415254
prefprejsoncontent = fp.read(prefjsonsize).decode("UTF-8")
52425255
fp.seek(len(delimiter), 1)
52435256
prejsonfcs = GetFileChecksum(prefprejsoncontent, prefjsonchecksumtype, True, formatspecs)
5244-
if(not hmac.compare_digest(prefjsonchecksum, prejsonfcs) and not skipchecksum):
5257+
if(not CheckChecksums(prefjsonchecksum, prejsonfcs) and not skipchecksum):
52455258
VerbosePrintOut("File JSON Data Checksum Error with file " +
52465259
prefname + " at offset " + str(prefhstart))
52475260
VerbosePrintOut("'" + prefjsonchecksum + "' != " + "'" + prejsonfcs + "'")
52485261
return False
52495262
prenewfcs = GetHeaderChecksum(
52505263
preheaderdata[:-2], preheaderdata[-4].lower(), True, formatspecs)
52515264
prefcs = preheaderdata[-2]
5252-
if(not hmac.compare_digest(prefcs, prenewfcs) and not skipchecksum):
5265+
if(not CheckChecksums(prefcs, prenewfcs) and not skipchecksum):
52535266
VerbosePrintOut("File Header Checksum Error with file " +
52545267
prefname + " at offset " + str(prefhstart))
52555268
VerbosePrintOut("'" + prefcs + "' != " +
@@ -5270,7 +5283,7 @@ def ReadFileDataWithContentToList(fp, filestart=0, seekstart=0, seekend=0, listo
52705283
prefcontents, preheaderdata[-3].lower(), False, formatspecs)
52715284
prefccs = preheaderdata[-1]
52725285
pyhascontents = True
5273-
if(not hmac.compare_digest(prefccs, prenewfccs) and not skipchecksum):
5286+
if(not CheckChecksums(prefccs, prenewfccs) and not skipchecksum):
52745287
VerbosePrintOut("File Content Checksum Error with file " +
52755288
prefname + " at offset " + str(prefcontentstart))
52765289
VerbosePrintOut("'" + prefccs +
@@ -9726,7 +9739,7 @@ def ArchiveFileValidate(infile, fmttype="auto", filestart=0,
97269739
fprejsoncontent = fp.read(fjsonsize)
97279740
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
97289741
if(fjsonsize > 0):
9729-
if(hmac.compare_digest(jsonfcs, fjsonchecksum)):
9742+
if(CheckChecksums(jsonfcs, fjsonchecksum)):
97309743
if(verbose):
97319744
VerbosePrintOut("File JSON Data Checksum Passed at offset " + str(outfjstart))
97329745
VerbosePrintOut("'" + outfjsonchecksum + "' == " + "'" + injsonfcs + "'")
@@ -9736,7 +9749,7 @@ def ArchiveFileValidate(infile, fmttype="auto", filestart=0,
97369749
if(verbose):
97379750
VerbosePrintOut("File JSON Data Checksum Error at offset " + str(outfjstart))
97389751
VerbosePrintOut("'" + outfjsonchecksum + "' != " + "'" + injsonfcs + "'")
9739-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
9752+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
97409753
VerbosePrintOut("File JSON Data Checksum Error with file " +
97419754
fname + " at offset " + str(fheaderstart))
97429755
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -9835,7 +9848,7 @@ def ArchiveFileValidate(infile, fmttype="auto", filestart=0,
98359848
VerbosePrintOut(outfname)
98369849
VerbosePrintOut("Record Number " + str(il) + "; File ID " + str(fid) + "; iNode Number " + str(finode))
98379850

9838-
if(hmac.compare_digest(outfcs, infcs)):
9851+
if(CheckChecksums(outfcs, infcs)):
98399852
if(verbose):
98409853
VerbosePrintOut("File Header Checksum Passed at offset " + str(outfhstart))
98419854
VerbosePrintOut("'" + outfcs + "' == " + "'" + infcs + "'")
@@ -9846,7 +9859,7 @@ def ArchiveFileValidate(infile, fmttype="auto", filestart=0,
98469859
VerbosePrintOut("File Header Checksum Failed at offset " + str(outfhstart))
98479860
VerbosePrintOut("'" + outfcs + "' != " + "'" + infcs + "'")
98489861
if(outfjsonsize > 0):
9849-
if(hmac.compare_digest(injsonfcs, outfjsonchecksum)):
9862+
if(CheckChecksums(injsonfcs, outfjsonchecksum)):
98509863
if(verbose):
98519864
VerbosePrintOut("File JSON Data Checksum Passed at offset " + str(outfjstart))
98529865
VerbosePrintOut("'" + outfjsonchecksum + "' == " + "'" + injsonfcs + "'")
@@ -9868,7 +9881,7 @@ def ArchiveFileValidate(infile, fmttype="auto", filestart=0,
98689881
infccs = GetFileChecksum(outfcontents, inheaderdata[-3].lower(), False, formatspecs)
98699882
pyhascontents = True
98709883

9871-
if(hmac.compare_digest(outfccs, infccs)):
9884+
if(CheckChecksums(outfccs, infccs)):
98729885
if(verbose):
98739886
VerbosePrintOut("File Content Checksum Passed at offset " + str(outfcontentstart))
98749887
VerbosePrintOut("'" + outfccs + "' == " + "'" + infccs + "'")

0 commit comments

Comments
 (0)