diff options
| author | J08nY | 2022-10-24 18:50:08 -0400 |
|---|---|---|
| committer | J08nY | 2022-10-24 18:50:08 -0400 |
| commit | b8a1fa1dba966730cdc3bd08219ef1429a2bdc97 (patch) | |
| tree | 864561bf7fa065367827a95f3ecd75af91aa2b67 | |
| parent | 0443f5360826147aa42f1f8b70d7c10ea8d44173 (diff) | |
| download | sec-certs-b8a1fa1dba966730cdc3bd08219ef1429a2bdc97.tar.gz sec-certs-b8a1fa1dba966730cdc3bd08219ef1429a2bdc97.tar.zst sec-certs-b8a1fa1dba966730cdc3bd08219ef1429a2bdc97.zip | |
Fix FipsAlgo sorting for serialization.
| -rw-r--r-- | sec_certs/sample/fips_algorithm.py | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/sec_certs/sample/fips_algorithm.py b/sec_certs/sample/fips_algorithm.py index ea74cd33..3d9e2e8b 100644 --- a/sec_certs/sample/fips_algorithm.py +++ b/sec_certs/sample/fips_algorithm.py @@ -1,11 +1,13 @@ from dataclasses import dataclass, field +from functools import total_ordering from typing import Optional from sec_certs import constants from sec_certs.serialization.json import ComplexSerializableType -@dataclass(eq=True, order=True, frozen=True) +@dataclass(eq=True, frozen=True) +@total_ordering class FIPSAlgorithm(ComplexSerializableType): """ Data structure for algorithm of `FIPSCertificate` @@ -19,14 +21,28 @@ class FIPSAlgorithm(ComplexSerializableType): @property def dgst(self) -> str: - return f"{self.algorithm_type}#{self.cert_id}" + return f"{self.algorithm_type}{self.cert_id}" @property def page_url(self) -> str: return constants.FIPS_ALG_URL.format(self.algorithm_type, self.cert_id) + def _compare_tuple(self): + return ( + self.cert_id, + self.algorithm_type if self.algorithm_type else "", + self.vendor if self.vendor else "", + self.implementation if self.implementation else "", + self.date if self.date else "", + ) + + def __lt__(self, other): + if not isinstance(other, FIPSAlgorithm): + raise ValueError("Cannot compare.") + return self._compare_tuple() < other._compare_tuple() + def __repr__(self) -> str: return f"FIPSAlgorithm({self.dgst})" def __str__(self) -> str: - return f"{self.algorithm_type} algorithm # {self.cert_id} created by {self.vendor}" + return f"{self.algorithm_type} algorithm {self.cert_id} created by {self.vendor}" |
