implement format_fingerprint method

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-03-21 01:16:07 +01:00
parent b4a03e3034
commit 5a9daa9db7
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -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.')