File: cipher.py

package info (click to toggle)
python-noiseprotocol 0.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,656 kB
  • sloc: python: 1,259; makefile: 25
file content (27 lines) | stat: -rw-r--r-- 610 bytes parent folder | download
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
import abc

from noiseprotocol.constants import MAX_NONCE


class Cipher(metaclass=abc.ABCMeta):
    def __init__(self):
        self.cipher = None

    @property
    @abc.abstractmethod
    def klass(self):
        raise NotImplementedError

    @abc.abstractmethod
    def encrypt(self, k, n, ad, plaintext):
        raise NotImplementedError

    @abc.abstractmethod
    def decrypt(self, k, n, ad, ciphertext):
        raise NotImplementedError

    def rekey(self, k):
        return self.encrypt(k, MAX_NONCE, b'', b'\x00' * 32)[:32]

    def initialize(self, key):
        self.cipher = self.klass(key)