Skip to content

Commit d04760d

Browse files
authored
Add files via upload
1 parent 74d180e commit d04760d

1 file changed

Lines changed: 34 additions & 21 deletions

File tree

pyfoxfile.py

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

38293829
def ValidateFileChecksum(infile, checksumtype="md5", inchecksum="0", formatspecs=__file_format_dict__, saltkey=None):
38303830
calc = GetFileChecksum(infile, checksumtype, True, formatspecs, saltkey)
38313831
want = (inchecksum or "0").strip().lower()
38323832
if want.startswith("0x"):
38333833
want = want[2:]
3834-
return hmac.compare_digest(want, calc)
3834+
return CheckChecksums(want, calc)
38353835

3836+
def CheckChecksums(inchecksum, outchecksum):
3837+
# Normalize as text first
3838+
calc = (inchecksum or "0").strip().lower()
3839+
want = (outchecksum or "0").strip().lower()
3840+
3841+
if want.startswith("0x"):
3842+
want = want[2:]
3843+
3844+
# Now force both to bytes
3845+
calc_b = _to_bytes(calc) # defaults to utf-8, strict
3846+
want_b = _to_bytes(want)
3847+
3848+
return hmac.compare_digest(want_b, calc_b)
38363849

38373850
def MajorMinorToDev(major, minor):
38383851
"""
@@ -4294,7 +4307,7 @@ def ReadFileHeaderDataWithContent(fp, listonly=False, uncompress=True, skipcheck
42944307
pass
42954308
fp.seek(len(delimiter), 1)
42964309
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
4297-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
4310+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
42984311
VerbosePrintOut("File JSON Data Checksum Error with file " +
42994312
fname + " at offset " + str(fheaderstart))
43004313
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -4323,7 +4336,7 @@ def ReadFileHeaderDataWithContent(fp, listonly=False, uncompress=True, skipcheck
43234336
newfccs = GetFileChecksum(
43244337
fcontents, HeaderOut[-3].lower(), False, formatspecs)
43254338
fcontents.seek(0, 0)
4326-
if(not hmac.compare_digest(fccs, newfccs) and not skipchecksum and not listonly):
4339+
if(not CheckChecksums(fccs, newfccs) and not skipchecksum and not listonly):
43274340
VerbosePrintOut("File Content Checksum Error with file " +
43284341
fname + " at offset " + str(fcontentstart))
43294342
VerbosePrintOut("'" + fccs + "' != " + "'" + newfccs + "'")
@@ -4506,7 +4519,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
45064519
fp.seek(len(delimiter), 1)
45074520
fjend = fp.tell() - 1
45084521
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
4509-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
4522+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
45104523
VerbosePrintOut("File JSON Data Checksum Error with file " +
45114524
fname + " at offset " + str(fheaderstart))
45124525
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -4540,7 +4553,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
45404553
newfccs = GetFileChecksum(
45414554
fcontents, HeaderOut[-3].lower(), False, formatspecs)
45424555
fcontents.seek(0, 0)
4543-
if(not hmac.compare_digest(fccs, newfccs) and not skipchecksum and not listonly):
4556+
if(not CheckChecksums(fccs, newfccs) and not skipchecksum and not listonly):
45444557
VerbosePrintOut("File Content Checksum Error with file " +
45454558
fname + " at offset " + str(fcontentstart))
45464559
VerbosePrintOut("'" + fccs + "' != " + "'" + newfccs + "'")
@@ -4720,7 +4733,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
47204733
pass
47214734
fp.seek(len(delimiter), 1)
47224735
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
4723-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
4736+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
47244737
VerbosePrintOut("File JSON Data Checksum Error with file " +
47254738
fname + " at offset " + str(fheaderstart))
47264739
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -4753,7 +4766,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
47534766
fcontents.seek(0, 0)
47544767
newfccs = GetFileChecksum(
47554768
fcontents, HeaderOut[-3].lower(), False, formatspecs)
4756-
if(not hmac.compare_digest(fccs, newfccs) and not skipchecksum and not listonly):
4769+
if(not CheckChecksums(fccs, newfccs) and not skipchecksum and not listonly):
47574770
VerbosePrintOut("File Content Checksum Error with file " +
47584771
fname + " at offset " + str(fcontentstart))
47594772
VerbosePrintOut("'" + fccs + "' != " + "'" + newfccs + "'")
@@ -5013,7 +5026,7 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
50135026
else:
50145027
return False
50155028
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
5016-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
5029+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
50175030
VerbosePrintOut("File JSON Data Checksum Error with file " +
50185031
fname + " at offset " + str(fheaderstart))
50195032
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -5058,15 +5071,15 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
50585071
prejsoncontent = fp.read(prefjsonsize).decode("UTF-8")
50595072
fp.seek(len(delimiter), 1)
50605073
prejsonfcs = GetFileChecksum(prejsoncontent, prefjsonchecksumtype, True, formatspecs)
5061-
if(not hmac.compare_digest(prefjsonchecksum, prejsonfcs) and not skipchecksum):
5074+
if(not CheckChecksums(prefjsonchecksum, prejsonfcs) and not skipchecksum):
50625075
VerbosePrintOut("File JSON Data Checksum Error with file " +
50635076
prefname + " at offset " + str(prefhstart))
50645077
VerbosePrintOut("'" + prefjsonchecksum + "' != " + "'" + prejsonfcs + "'")
50655078
return False
50665079
prenewfcs = GetHeaderChecksum(
50675080
preheaderdata[:-2], preheaderdata[-4].lower(), True, formatspecs)
50685081
prefcs = preheaderdata[-2]
5069-
if(not hmac.compare_digest(prefcs, prenewfcs) and not skipchecksum):
5082+
if(not CheckChecksums(prefcs, prenewfcs) and not skipchecksum):
50705083
VerbosePrintOut("File Header Checksum Error with file " +
50715084
prefname + " at offset " + str(prefhstart))
50725085
VerbosePrintOut("'" + prefcs + "' != " +
@@ -5085,7 +5098,7 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
50855098
prefcontents, preheaderdata[-3].lower(), False, formatspecs)
50865099
prefccs = preheaderdata[-1]
50875100
pyhascontents = True
5088-
if(not hmac.compare_digest(prefccs, prenewfccs) and not skipchecksum):
5101+
if(not CheckChecksums(prefccs, prenewfccs) and not skipchecksum):
50895102
VerbosePrintOut("File Content Checksum Error with file " +
50905103
prefname + " at offset " + str(prefcontentstart))
50915104
VerbosePrintOut("'" + prefccs +
@@ -5199,7 +5212,7 @@ def ReadFileDataWithContentToList(fp, filestart=0, seekstart=0, seekend=0, listo
51995212
else:
52005213
return False
52015214
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
5202-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
5215+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
52035216
VerbosePrintOut("File JSON Data Checksum Error with file " +
52045217
fname + " at offset " + str(fheaderstart))
52055218
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -5249,15 +5262,15 @@ def ReadFileDataWithContentToList(fp, filestart=0, seekstart=0, seekend=0, listo
52495262
prefprejsoncontent = fp.read(prefjsonsize).decode("UTF-8")
52505263
fp.seek(len(delimiter), 1)
52515264
prejsonfcs = GetFileChecksum(prefprejsoncontent, prefjsonchecksumtype, True, formatspecs)
5252-
if(not hmac.compare_digest(prefjsonchecksum, prejsonfcs) and not skipchecksum):
5265+
if(not CheckChecksums(prefjsonchecksum, prejsonfcs) and not skipchecksum):
52535266
VerbosePrintOut("File JSON Data Checksum Error with file " +
52545267
prefname + " at offset " + str(prefhstart))
52555268
VerbosePrintOut("'" + prefjsonchecksum + "' != " + "'" + prejsonfcs + "'")
52565269
return False
52575270
prenewfcs = GetHeaderChecksum(
52585271
preheaderdata[:-2], preheaderdata[-4].lower(), True, formatspecs)
52595272
prefcs = preheaderdata[-2]
5260-
if(not hmac.compare_digest(prefcs, prenewfcs) and not skipchecksum):
5273+
if(not CheckChecksums(prefcs, prenewfcs) and not skipchecksum):
52615274
VerbosePrintOut("File Header Checksum Error with file " +
52625275
prefname + " at offset " + str(prefhstart))
52635276
VerbosePrintOut("'" + prefcs + "' != " +
@@ -5278,7 +5291,7 @@ def ReadFileDataWithContentToList(fp, filestart=0, seekstart=0, seekend=0, listo
52785291
prefcontents, preheaderdata[-3].lower(), False, formatspecs)
52795292
prefccs = preheaderdata[-1]
52805293
pyhascontents = True
5281-
if(not hmac.compare_digest(prefccs, prenewfccs) and not skipchecksum):
5294+
if(not CheckChecksums(prefccs, prenewfccs) and not skipchecksum):
52825295
VerbosePrintOut("File Content Checksum Error with file " +
52835296
prefname + " at offset " + str(prefcontentstart))
52845297
VerbosePrintOut("'" + prefccs +
@@ -9732,7 +9745,7 @@ def FoxFileValidate(infile, fmttype="auto", filestart=0,
97329745
fprejsoncontent = fp.read(fjsonsize)
97339746
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
97349747
if(fjsonsize > 0):
9735-
if(hmac.compare_digest(jsonfcs, fjsonchecksum)):
9748+
if(CheckChecksums(jsonfcs, fjsonchecksum)):
97369749
if(verbose):
97379750
VerbosePrintOut("File JSON Data Checksum Passed at offset " + str(outfjstart))
97389751
VerbosePrintOut("'" + outfjsonchecksum + "' == " + "'" + injsonfcs + "'")
@@ -9742,7 +9755,7 @@ def FoxFileValidate(infile, fmttype="auto", filestart=0,
97429755
if(verbose):
97439756
VerbosePrintOut("File JSON Data Checksum Error at offset " + str(outfjstart))
97449757
VerbosePrintOut("'" + outfjsonchecksum + "' != " + "'" + injsonfcs + "'")
9745-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
9758+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
97469759
VerbosePrintOut("File JSON Data Checksum Error with file " +
97479760
fname + " at offset " + str(fheaderstart))
97489761
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -9841,7 +9854,7 @@ def FoxFileValidate(infile, fmttype="auto", filestart=0,
98419854
VerbosePrintOut(outfname)
98429855
VerbosePrintOut("Record Number " + str(il) + "; File ID " + str(fid) + "; iNode Number " + str(finode))
98439856

9844-
if(hmac.compare_digest(outfcs, infcs)):
9857+
if(CheckChecksums(outfcs, infcs)):
98459858
if(verbose):
98469859
VerbosePrintOut("File Header Checksum Passed at offset " + str(outfhstart))
98479860
VerbosePrintOut("'" + outfcs + "' == " + "'" + infcs + "'")
@@ -9852,7 +9865,7 @@ def FoxFileValidate(infile, fmttype="auto", filestart=0,
98529865
VerbosePrintOut("File Header Checksum Failed at offset " + str(outfhstart))
98539866
VerbosePrintOut("'" + outfcs + "' != " + "'" + infcs + "'")
98549867
if(outfjsonsize > 0):
9855-
if(hmac.compare_digest(injsonfcs, outfjsonchecksum)):
9868+
if(CheckChecksums(injsonfcs, outfjsonchecksum)):
98569869
if(verbose):
98579870
VerbosePrintOut("File JSON Data Checksum Passed at offset " + str(outfjstart))
98589871
VerbosePrintOut("'" + outfjsonchecksum + "' == " + "'" + injsonfcs + "'")
@@ -9874,7 +9887,7 @@ def FoxFileValidate(infile, fmttype="auto", filestart=0,
98749887
infccs = GetFileChecksum(outfcontents, inheaderdata[-3].lower(), False, formatspecs)
98759888
pyhascontents = True
98769889

9877-
if(hmac.compare_digest(outfccs, infccs)):
9890+
if(CheckChecksums(outfccs, infccs)):
98789891
if(verbose):
98799892
VerbosePrintOut("File Content Checksum Passed at offset " + str(outfcontentstart))
98809893
VerbosePrintOut("'" + outfccs + "' == " + "'" + infccs + "'")

0 commit comments

Comments
 (0)