From 5a9daa9db73f9d26dc859fdd0f398a001af01e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 21 Mar 2022 01:16:07 +0100 Subject: [PATCH] implement format_fingerprint method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- poezio_omemo/__init__.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/poezio_omemo/__init__.py b/poezio_omemo/__init__.py index 3209d2f..87fc9e5 100644 --- a/poezio_omemo/__init__.py +++ b/poezio_omemo/__init__.py @@ -18,8 +18,10 @@ import logging from pathlib import Path from typing import Dict, List, Optional +from poezio import colors from poezio.plugin_e2ee import E2EEPlugin from poezio.xdg import DATA_HOME +from poezio.theming import Theme from poezio.tabs import ChatTab, DynamicConversationTab, StaticConversationTab, MucTab from omemo.exceptions import MissingBundleException @@ -123,6 +125,31 @@ class Plugin(E2EEPlugin): if trust is not None ] + @staticmethod + def format_fingerprint(fingerprint: str, theme: Theme) -> str: + """ + Color fingerprint as specified in in XEP-0384 0.8.3 “§8 Security + Considerations”. + + “When displaying the fingerprint as a hex-string, the RECOMMENDED + way to make it easier to compare the fingerprint is to split the + lowercase hex-string into 8 substrings of 8 chars each, then + coloring each group of 8 lowercase hex chars using Consistent + Color Generation (XEP-0392)” + """ + size = len(fingerprint) // 8 + parts = map(''.join, zip(*[iter(fingerprint)]*8)) + colored_fp = '' + for i, part in enumerate(parts): + fg_color = colors.ccg_text_to_color(theme.ccg_palette, part) + separator = ' ' + if i == (size // 2 - 1): + separator = '\n' + elif i == size - 1: + separator = '' + colored_fp += f'\x19{fg_color}}}{part}{separator}' + return colored_fp + async def decrypt(self, message: Message, jid: Optional[JID], tab: ChatTab) -> None: if jid is None: self.display_error('Unable to decrypt the message.')