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

util.py « omemo « common « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 13c4fdc80e9289ce436921d37bd11052c5f9c1f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright (C) 2015 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
#
# This file is part of OMEMO Gajim Plugin.
#
# OMEMO Gajim Plugin is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# OMEMO Gajim Plugin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OMEMO Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

import binascii
import textwrap
from enum import IntEnum

from omemo_dr.identitykey import IdentityKey
from omemo_dr.identitykeypair import IdentityKeyPair

DEFAULT_PREKEY_AMOUNT = 100
MIN_PREKEY_AMOUNT = 80
SPK_ARCHIVE_TIME = 86400 * 15  # 15 Days
SPK_CYCLE_TIME = 86400         # 24 Hours
UNACKNOWLEDGED_COUNT = 2000


class Trust(IntEnum):
    UNTRUSTED = 0
    VERIFIED = 1
    UNDECIDED = 2
    BLIND = 3


def get_fingerprint(identity_key: IdentityKeyPair,
                    formatted: bool = False
                    ) -> str:

    public_key = identity_key.getPublicKey().serialize()
    fingerprint = binascii.hexlify(public_key).decode()[2:]
    if not formatted:
        return fingerprint
    fplen = len(fingerprint)
    wordsize = fplen // 8
    buf = ''
    for word in range(0, fplen, wordsize):
        buf += '{0} '.format(fingerprint[word:word + wordsize])
    buf = textwrap.fill(buf, width=36)
    return buf.rstrip().upper()


class IdentityKeyExtended(IdentityKey):
    def __hash__(self) -> int:
        return hash(self.publicKey.serialize())

    def get_fingerprint(self, formatted: bool = False) -> str:
        return get_fingerprint(self, formatted=formatted)