Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Hörist <philipp@hoerist.com>2023-05-28 20:44:51 +0300
committerPhilipp Hörist <philipp@hoerist.com>2023-05-28 22:12:32 +0300
commitfbb470b771390f8328c657f8fc2635917a608d3c (patch)
treedafaa0d7ef96dfcf9ab48211109d23c4d4a67134
parented674b8cb125c98d068ca75c418a1164bae05b0d (diff)
refactor: Add util methods for sha formatting
-rw-r--r--gajim/common/util/text.py17
-rw-r--r--gajim/gtk/certificate_dialog.py8
2 files changed, 20 insertions, 5 deletions
diff --git a/gajim/common/util/text.py b/gajim/common/util/text.py
index a37ceb965..2ab79dde9 100644
--- a/gajim/common/util/text.py
+++ b/gajim/common/util/text.py
@@ -14,6 +14,8 @@
from __future__ import annotations
+from typing import Literal
+
import re
from gi.repository import GLib
@@ -70,3 +72,18 @@ def format_duration(ns: float, total_ns: float) -> str:
return (f'%0{width}d' % i_minutes) + f':{i_seconds:02d}'
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}')
+
+ 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]))
+ return '\n'.join(lines)
diff --git a/gajim/gtk/certificate_dialog.py b/gajim/gtk/certificate_dialog.py
index 8a938d6ea..ae46a16c3 100644
--- a/gajim/gtk/certificate_dialog.py
+++ b/gajim/gtk/certificate_dialog.py
@@ -29,6 +29,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.gtk.builder import get_builder
@@ -109,13 +110,10 @@ class CertificateBox(Gtk.Box):
self._expires = cert.not_valid_after.strftime('%c %Z')
sha1_bytes = cert.fingerprint(hashes.SHA1())
- sha1 = ':'.join(f'{b:02X}' for b in sha1_bytes)
- self._sha1 = '%s\n%s' % (sha1[:29], sha1[30:])
+ self._sha1 = format_sha_bytes('sha1', sha1_bytes)
sha256_bytes = cert.fingerprint(hashes.SHA256())
- sha256 = ':'.join(f'{b:02X}' for b in sha256_bytes)
- self._sha256 = '%s\n%s\n%s\n%s' % (
- sha256[:23], sha256[24:47], sha256[48:71], sha256[72:])
+ self._sha256 = format_sha_bytes('sha256', sha256_bytes)
public_key = cert.public_key()
self._pk_algorithm = ''