diff options
author | Philipp Hörist <philipp@hoerist.com> | 2023-08-29 14:40:06 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2023-08-29 14:40:06 +0300 |
commit | 4830ef3aea68fce850726f90867165337ae3f5b0 (patch) | |
tree | 8a5047d34e0f7b5a6ee005b61a24ba835f54fbca | |
parent | 4f768a5fa292350ae0c018bfe2d3a2835b51540b (diff) |
refactor: Make format_bytes_as_hex more generic
-rw-r--r-- | gajim/common/util/text.py | 19 | ||||
-rw-r--r-- | gajim/gtk/certificate_dialog.py | 14 | ||||
-rw-r--r-- | test/common/test_text_util.py | 12 |
3 files changed, 26 insertions, 19 deletions
diff --git a/gajim/common/util/text.py b/gajim/common/util/text.py index 2ab79dde9..257f358d4 100644 --- a/gajim/common/util/text.py +++ b/gajim/common/util/text.py @@ -14,8 +14,7 @@ from __future__ import annotations -from typing import Literal - +import math import re from gi.repository import GLib @@ -74,16 +73,12 @@ def format_duration(ns: float, total_ns: float) -> str: return f'0:{i_seconds:02d}' -def format_sha_bytes(algo: Literal['sha1', 'sha256'], sha_bytes: bytes) -> str: - if algo == 'sha1': - stop, step = 20, 10 - elif algo == 'sha256': - stop, step = 32, 8 - else: - raise ValueError(f'Unknown algo: {algo}') +def format_bytes_as_hex(bytes_: bytes, line_count: int = 1) -> str: + line_length = math.ceil(len(bytes_) / line_count) + + hex_list = [f'{b:02X}' for b in bytes_] lines: list[str] = [] - hex_list = [f'{b:02X}' for b in sha_bytes] - for pos in range(0, stop, step): - lines.append(':'.join(hex_list[pos:pos + step])) + for pos in range(0, len(bytes_), line_length): + lines.append(':'.join(hex_list[pos:pos + line_length])) return '\n'.join(lines) diff --git a/gajim/gtk/certificate_dialog.py b/gajim/gtk/certificate_dialog.py index 046956ee7..060500f7d 100644 --- a/gajim/gtk/certificate_dialog.py +++ b/gajim/gtk/certificate_dialog.py @@ -22,6 +22,7 @@ from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey from cryptography.hazmat.primitives.asymmetric.ed448 import Ed448PublicKey from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey +from cryptography.utils import int_to_bytes from cryptography.x509 import DNSName from cryptography.x509 import ExtensionNotFound from cryptography.x509.oid import ExtensionOID @@ -32,7 +33,7 @@ from gi.repository import Gtk from gajim.common import app from gajim.common.helpers import get_x509_cert_from_gio_cert from gajim.common.i18n import _ -from gajim.common.util.text import format_sha_bytes +from gajim.common.util.text import format_bytes_as_hex from gajim.gtk.builder import get_builder @@ -101,10 +102,9 @@ class CertificateBox(Gtk.Box): log.info('Certificate does not have extension: %s', err) self._it_subject_alt_names = '' - serial_str = f'{cert.serial_number:02X}' - serial_str_foratted = ':'.join( - map('{}{}'.format, *(serial_str[::2], serial_str[1::2]))) - self._it_serial_number = serial_str_foratted + serial_bytes = format_bytes_as_hex( + int_to_bytes(cert.serial_number), 2) + self._it_serial_number = serial_bytes self._ib_common_name = '' self._ib_organization = '' @@ -120,10 +120,10 @@ class CertificateBox(Gtk.Box): self._expires = cert.not_valid_after.strftime('%c %Z') sha1_bytes = cert.fingerprint(hashes.SHA1()) - self._sha1 = format_sha_bytes('sha1', sha1_bytes) + self._sha1 = format_bytes_as_hex(sha1_bytes, 2) sha256_bytes = cert.fingerprint(hashes.SHA256()) - self._sha256 = format_sha_bytes('sha256', sha256_bytes) + self._sha256 = format_bytes_as_hex(sha256_bytes, 4) public_key = cert.public_key() self._pk_algorithm = '' diff --git a/test/common/test_text_util.py b/test/common/test_text_util.py index 7e984d1f6..0f3bf92ed 100644 --- a/test/common/test_text_util.py +++ b/test/common/test_text_util.py @@ -1,6 +1,7 @@ import unittest from gajim.common.util.text import escape_iri_path_segment +from gajim.common.util.text import format_bytes_as_hex from gajim.common.util.text import format_duration from gajim.common.util.text import jid_to_iri from gajim.common.util.text import remove_invalid_xml_chars @@ -72,6 +73,17 @@ class Test(unittest.TestCase): do(3600000000000.0, '001:00:00') do(3599999999999999.0, '999:59:59') + def test_format_bytes_as_hex(self) -> None: + tests = [ + (b'U\xe9\xf4\xd6~\x17\xa6\xdf]\xc7\xfe\x9a\x01\r\x8b\xea\xfd\x8f@.', + '55:E9:F4:D6:7E:17:A6:DF:5D:C7\nFE:9A:01:0D:8B:EA:FD:8F:40:2E'), + ] + + for test in tests: + bytes_, string = test + hex_string = format_bytes_as_hex(bytes_, 2) + self.assertEqual(hex_string, string) + if __name__ == '__main__': unittest.main() |