Skip to content

Commit 3177c71

Browse files
authored
Add files via upload
1 parent 2eb24ec commit 3177c71

1 file changed

Lines changed: 34 additions & 21 deletions

File tree

pycatfile.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,15 +3823,28 @@ def ValidateHeaderChecksum(inlist=None, checksumtype="md5", inchecksum="0", form
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

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

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

38363849
def MajorMinorToDev(major, minor):
38373850
"""
@@ -4293,7 +4306,7 @@ def ReadFileHeaderDataWithContent(fp, listonly=False, uncompress=True, skipcheck
42934306
pass
42944307
fp.seek(len(delimiter), 1)
42954308
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
4296-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
4309+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
42974310
VerbosePrintOut("File JSON Data Checksum Error with file " +
42984311
fname + " at offset " + str(fheaderstart))
42994312
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -4322,7 +4335,7 @@ def ReadFileHeaderDataWithContent(fp, listonly=False, uncompress=True, skipcheck
43224335
newfccs = GetFileChecksum(
43234336
fcontents, HeaderOut[-3].lower(), False, formatspecs)
43244337
fcontents.seek(0, 0)
4325-
if(not hmac.compare_digest(fccs, newfccs) and not skipchecksum and not listonly):
4338+
if(not CheckChecksums(fccs, newfccs) and not skipchecksum and not listonly):
43264339
VerbosePrintOut("File Content Checksum Error with file " +
43274340
fname + " at offset " + str(fcontentstart))
43284341
VerbosePrintOut("'" + fccs + "' != " + "'" + newfccs + "'")
@@ -4505,7 +4518,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
45054518
fp.seek(len(delimiter), 1)
45064519
fjend = fp.tell() - 1
45074520
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
4508-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
4521+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
45094522
VerbosePrintOut("File JSON Data Checksum Error with file " +
45104523
fname + " at offset " + str(fheaderstart))
45114524
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -4539,7 +4552,7 @@ def ReadFileHeaderDataWithContentToArray(fp, listonly=False, contentasfile=True,
45394552
newfccs = GetFileChecksum(
45404553
fcontents, HeaderOut[-3].lower(), False, formatspecs)
45414554
fcontents.seek(0, 0)
4542-
if(not hmac.compare_digest(fccs, newfccs) and not skipchecksum and not listonly):
4555+
if(not CheckChecksums(fccs, newfccs) and not skipchecksum and not listonly):
45434556
VerbosePrintOut("File Content Checksum Error with file " +
45444557
fname + " at offset " + str(fcontentstart))
45454558
VerbosePrintOut("'" + fccs + "' != " + "'" + newfccs + "'")
@@ -4719,7 +4732,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
47194732
pass
47204733
fp.seek(len(delimiter), 1)
47214734
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
4722-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
4735+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
47234736
VerbosePrintOut("File JSON Data Checksum Error with file " +
47244737
fname + " at offset " + str(fheaderstart))
47254738
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -4752,7 +4765,7 @@ def ReadFileHeaderDataWithContentToList(fp, listonly=False, contentasfile=False,
47524765
fcontents.seek(0, 0)
47534766
newfccs = GetFileChecksum(
47544767
fcontents, HeaderOut[-3].lower(), False, formatspecs)
4755-
if(not hmac.compare_digest(fccs, newfccs) and not skipchecksum and not listonly):
4768+
if(not CheckChecksums(fccs, newfccs) and not skipchecksum and not listonly):
47564769
VerbosePrintOut("File Content Checksum Error with file " +
47574770
fname + " at offset " + str(fcontentstart))
47584771
VerbosePrintOut("'" + fccs + "' != " + "'" + newfccs + "'")
@@ -5012,7 +5025,7 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
50125025
else:
50135026
return False
50145027
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
5015-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
5028+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
50165029
VerbosePrintOut("File JSON Data Checksum Error with file " +
50175030
fname + " at offset " + str(fheaderstart))
50185031
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -5057,15 +5070,15 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
50575070
prejsoncontent = fp.read(prefjsonsize).decode("UTF-8")
50585071
fp.seek(len(delimiter), 1)
50595072
prejsonfcs = GetFileChecksum(prejsoncontent, prefjsonchecksumtype, True, formatspecs)
5060-
if(not hmac.compare_digest(prefjsonchecksum, prejsonfcs) and not skipchecksum):
5073+
if(not CheckChecksums(prefjsonchecksum, prejsonfcs) and not skipchecksum):
50615074
VerbosePrintOut("File JSON Data Checksum Error with file " +
50625075
prefname + " at offset " + str(prefhstart))
50635076
VerbosePrintOut("'" + prefjsonchecksum + "' != " + "'" + prejsonfcs + "'")
50645077
return False
50655078
prenewfcs = GetHeaderChecksum(
50665079
preheaderdata[:-2], preheaderdata[-4].lower(), True, formatspecs)
50675080
prefcs = preheaderdata[-2]
5068-
if(not hmac.compare_digest(prefcs, prenewfcs) and not skipchecksum):
5081+
if(not CheckChecksums(prefcs, prenewfcs) and not skipchecksum):
50695082
VerbosePrintOut("File Header Checksum Error with file " +
50705083
prefname + " at offset " + str(prefhstart))
50715084
VerbosePrintOut("'" + prefcs + "' != " +
@@ -5084,7 +5097,7 @@ def ReadFileDataWithContentToArray(fp, filestart=0, seekstart=0, seekend=0, list
50845097
prefcontents, preheaderdata[-3].lower(), False, formatspecs)
50855098
prefccs = preheaderdata[-1]
50865099
pyhascontents = True
5087-
if(not hmac.compare_digest(prefccs, prenewfccs) and not skipchecksum):
5100+
if(not CheckChecksums(prefccs, prenewfccs) and not skipchecksum):
50885101
VerbosePrintOut("File Content Checksum Error with file " +
50895102
prefname + " at offset " + str(prefcontentstart))
50905103
VerbosePrintOut("'" + prefccs +
@@ -5198,7 +5211,7 @@ def ReadFileDataWithContentToList(fp, filestart=0, seekstart=0, seekend=0, listo
51985211
else:
51995212
return False
52005213
jsonfcs = GetFileChecksum(fprejsoncontent, fjsonchecksumtype, True, formatspecs)
5201-
if(not hmac.compare_digest(fjsonchecksum, jsonfcs) and not skipchecksum):
5214+
if(not CheckChecksums(fjsonchecksum, jsonfcs) and not skipchecksum):
52025215
VerbosePrintOut("File JSON Data Checksum Error with file " +
52035216
fname + " at offset " + str(fheaderstart))
52045217
VerbosePrintOut("'" + fjsonchecksum + "' != " + "'" + jsonfcs + "'")
@@ -5248,15 +5261,15 @@ def ReadFileDataWithContentToList(fp, filestart=0, seekstart=0, seekend=0, listo
52485261
prefprejsoncontent = fp.read(prefjsonsize).decode("UTF-8")
52495262
fp.seek(len(delimiter), 1)
52505263
prejsonfcs = GetFileChecksum(prefprejsoncontent, prefjsonchecksumtype, True, formatspecs)
5251-
if(not hmac.compare_digest(prefjsonchecksum, prejsonfcs) and not skipchecksum):
5264+
if(not CheckChecksums(prefjsonchecksum, prejsonfcs) and not skipchecksum):
52525265
VerbosePrintOut("File JSON Data Checksum Error with file " +
52535266
prefname + " at offset " + str(prefhstart))
52545267
VerbosePrintOut("'" + prefjsonchecksum + "' != " + "'" + prejsonfcs + "'")
52555268
return False
52565269
prenewfcs = GetHeaderChecksum(
52575270
preheaderdata[:-2], preheaderdata[-4].lower(), True, formatspecs)
52585271
prefcs = preheaderdata[-2]
5259-
if(not hmac.compare_digest(prefcs, prenewfcs) and not skipchecksum):
5272+
if(not CheckChecksums(prefcs, prenewfcs) and not skipchecksum):
52605273
VerbosePrintOut("File Header Checksum Error with file " +
52615274
prefname + " at offset " + str(prefhstart))
52625275
VerbosePrintOut("'" + prefcs + "' != " +
@@ -5277,7 +5290,7 @@ def ReadFileDataWithContentToList(fp, filestart=0, seekstart=0, seekend=0, listo
52775290
prefcontents, preheaderdata[-3].lower(), False, formatspecs)
52785291
prefccs = preheaderdata[-1]
52795292
pyhascontents = True
5280-
if(not hmac.compare_digest(prefccs, prenewfccs) and not skipchecksum):
5293+
if(not CheckChecksums(prefccs, prenewfccs) and not skipchecksum):
52815294
VerbosePrintOut("File Content Checksum Error with file " +
52825295
prefname + " at offset " + str(prefcontentstart))
52835296
VerbosePrintOut("'" + prefccs +
@@ -9732,7 +9745,7 @@ def CatFileValidate(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 CatFileValidate(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 CatFileValidate(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 CatFileValidate(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 CatFileValidate(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)