Skip to content

Commit c89f401

Browse files
authored
Add files via upload
1 parent e86809d commit c89f401

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

pycatfile.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3673,7 +3673,7 @@ def _bytes_to_int(b):
36733673
# =========================
36743674
# Public checksum API
36753675
# =========================
3676-
def GetHeaderChecksum(inlist=None, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3676+
def GetHeaderChecksum(inlist=None, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__, saltkey=None):
36773677
"""
36783678
Serialize header fields (list/tuple => joined with delimiter + trailing delimiter;
36793679
or a single field) and compute the requested checksum. Returns lowercase hex.
@@ -3685,15 +3685,16 @@ def GetHeaderChecksum(inlist=None, checksumtype="md5", encodedata=True, formatsp
36853685
if encodedata and not isinstance(hdr_bytes, (bytes, bytearray, memoryview)):
36863686
hdr_bytes = _to_bytes(hdr_bytes)
36873687
hdr_bytes = bytes(hdr_bytes)
3688-
36893688
if CheckSumSupport(algo_key, hashlib_guaranteed):
3690-
h = hashlib.new(algo_key)
3691-
h.update(hdr_bytes)
3692-
return h.hexdigest().lower()
3689+
if(saltkey is None):
3690+
h = hashlib.new(algo_key, hdr_bytes)
3691+
else:
3692+
h = hmac.new(saltkey, hdr_bytes, digestmod=algo_key)
3693+
return h.hexdigest().lower()
36933694

36943695
return "0"
36953696

3696-
def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__):
3697+
def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__file_format_dict__, saltkey=None):
36973698
"""
36983699
Accepts bytes/str/file-like.
36993700
- Hashlib algos: streamed in 1 MiB chunks.
@@ -3707,7 +3708,10 @@ def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__
37073708
# hashlib
37083709

37093710
if CheckSumSupport(algo_key, hashlib_guaranteed):
3710-
h = hashlib.new(algo_key)
3711+
if(saltkey is None):
3712+
h = hashlib.new(algo_key)
3713+
else:
3714+
h = hmac.new(saltkey, digestmod=algo_key)
37113715
while True:
37123716
chunk = inbytes.read(__filebuff_size__)
37133717
if not chunk:
@@ -3728,8 +3732,10 @@ def GetFileChecksum(inbytes, checksumtype="md5", encodedata=True, formatspecs=__
37283732
# one-shot
37293733

37303734
if CheckSumSupport(algo_key, hashlib_guaranteed):
3731-
h = hashlib.new(algo_key)
3732-
h.update(data)
3735+
if(saltkey is None):
3736+
h = hashlib.new(algo_key, data)
3737+
else:
3738+
h = hmac.new(saltkey, data, digestmod=algo_key)
37333739
return h.hexdigest().lower()
37343740

37353741
return "0"

0 commit comments

Comments
 (0)