File: const_fernet.py

package info (click to toggle)
python-snitun 0.45.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 640 kB
  • sloc: python: 6,681; sh: 5; makefile: 3
file content (36 lines) | stat: -rw-r--r-- 857 bytes parent folder | download | duplicates (2)
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
"""Const value for Fernet tests."""

import json

from cryptography.fernet import Fernet, MultiFernet

from snitun import PROTOCOL_VERSION

FERNET_TOKENS = [
    "XIKL24X0Fu83UmPLmWkXOBvvqsLq41tz2LljwafDyZw=",
    "ep1FyYA6epwbFxrtEJ2dii5BGvTx5-xU1oUCrF61qMA=",
]


def create_peer_config(
    valid: int,
    hostname: str,
    aes_key: bytes,
    aes_iv: bytes,
    alias: list[str] | None = None,
) -> bytes:
    """Create a fernet token."""
    fernet = MultiFernet([Fernet(key) for key in FERNET_TOKENS])

    return fernet.encrypt(
        json.dumps(
            {
                "protocol_version": PROTOCOL_VERSION,
                "valid": valid,
                "hostname": hostname,
                "alias": alias or [],
                "aes_key": aes_key.hex(),
                "aes_iv": aes_iv.hex(),
            },
        ).encode(),
    )