1010import dataclasses
1111import datetime
1212import functools
13- import hashlib
1413import logging
1514import traceback
1615import xml .etree .ElementTree as ET
3736from vulnerabilities .severity_systems import SCORING_SYSTEMS
3837from vulnerabilities .severity_systems import ScoringSystem
3938from vulnerabilities .utils import classproperty
39+ from vulnerabilities .utils import compute_patch_checksum
4040from vulnerabilities .utils import get_reference_id
4141from vulnerabilities .utils import is_commit
4242from vulnerabilities .utils import is_cve
@@ -202,6 +202,7 @@ class PackageCommitPatchData:
202202 vcs_url : str
203203 commit_hash : str
204204 patch_text : Optional [str ] = None
205+ patch_checksum : Optional [str ] = dataclasses .field (init = False , default = None )
205206
206207 def __post_init__ (self ):
207208 if not self .commit_hash :
@@ -213,6 +214,9 @@ def __post_init__(self):
213214 if not self .vcs_url :
214215 raise ValueError ("Commit must have a non-empty vcs_url." )
215216
217+ if self .patch_text :
218+ self .patch_checksum = compute_patch_checksum (self .patch_text )
219+
216220 def __lt__ (self , other ):
217221 if not isinstance (other , PackageCommitPatchData ):
218222 return NotImplemented
@@ -224,6 +228,7 @@ def _cmp_key(self):
224228 self .vcs_url ,
225229 self .commit_hash ,
226230 self .patch_text ,
231+ self .patch_checksum ,
227232 )
228233
229234 def to_dict (self ) -> dict :
@@ -232,6 +237,7 @@ def to_dict(self) -> dict:
232237 "vcs_url" : self .vcs_url ,
233238 "commit_hash" : self .commit_hash ,
234239 "patch_text" : self .patch_text ,
240+ "patch_checksum" : self .patch_checksum ,
235241 }
236242
237243 @classmethod
@@ -256,7 +262,7 @@ def __post_init__(self):
256262 raise ValueError ("A patch must include either patch_url or patch_text" )
257263
258264 if self .patch_text :
259- self .patch_checksum = hashlib . sha512 (self .patch_text . encode ()). hexdigest ( )
265+ self .patch_checksum = compute_patch_checksum (self .patch_text )
260266
261267 def __lt__ (self , other ):
262268 if not isinstance (other , PatchData ):
0 commit comments