Nyx Node
Loading...
Searching...
No Matches
utils.py
1# -*- coding: utf-8 -*-
2########################################################################################################################
3# NyxNode
4# Author: Jérôme ODIER <jerome.odier@lpsc.in2p3.fr>
5# SPDX-License-Identifier: GPL-3.0+
6########################################################################################################################
7
8import ctypes
9import typing
10
11########################################################################################################################
12
13from . import bind
14
15########################################################################################################################
16
17## @defgroup UTILS_PY Nyx utilities
18# @brief Nyx utilities.
19
20########################################################################################################################
21
22def nyx_hash(string: str | bytes | None, seed: int) -> int:
23 """!
24 @ingroup UTILS_PY
25 @brief Hashes a string using the MurmurHash2 algorithm.
26
27 @param string String to hash.
28 @param seed Initial seed value.
29 @return The computed 32-bit hash.
30 """
31
32 if not string:
33
34 return int(seed) & 0xFFFFFFFF
35
36 ####################################################################################################################
37
38 data = bind.as_bytes(string, allow_none = False)
39
40 return int(bind.lib.nyx_hash(len(data), data, int(seed) & 0xFFFFFFFF))
41
42########################################################################################################################
43
44def nyx_generate_mac_addr(mac0: int, mac1: int, node_id: str) -> bytes:
45 """!
46 @ingroup UTILS_PY
47 @brief Generates a MAC address based on a node identifier.
48
49 @param mac0 First fixed byte of the MAC address.
50 @param mac1 Second fixed byte of the MAC address.
51 @param node_id Unique node identifier used to hash the remaining bytes.
52 @return The generated MAC address.
53 """
54
55 ####################################################################################################################
56
57 node_id = bind.as_bytes(node_id, allow_none = False)
58
59 ####################################################################################################################
60
61 result_mac = (bind.c_uint8 * 6)()
62
63 bind.lib.nyx_generate_mac_addr(
64 result_mac,
65 mac0 & 0xFF,
66 mac1 & 0xFF,
67 node_id
68 )
69
70 return bytes(result_mac)
71
72########################################################################################################################
73
74def nyx_base64_encode(data: bytes | None) -> str | None:
75 """!
76 @ingroup UTILS_PY
77 @brief Encodes a buffer using the Base64 algorithm.
78
79 @param data Data to encode.
80 @return The encoded string, or None when no data is provided.
81 """
82
83 if not data:
84
85 return None
86
87 ####################################################################################################################
88
89 data = bind.as_bytes(data, allow_none = False)
90
91 ####################################################################################################################
92
93 result_size = bind.c_size_t()
94
95 result_buff = bind.lib.nyx_base64_encode(ctypes.byref(result_size), len(data), data)
96
97 return bind.take_string(result_buff, result_size.value)
98
99########################################################################################################################
100
101def nyx_base64_decode(data: str | None) -> bytes | None:
102 """!
103 @ingroup UTILS_PY
104 @brief Decodes a string using the Base64 algorithm.
105
106 @param data Base64 data to decode.
107 @return The decoded buffer, or None when no data is provided.
108 """
109
110 if not str:
111
112 return None
113
114 ####################################################################################################################
115
116 data = bind.as_bytes(data, allow_none = False)
117
118 ####################################################################################################################
119
120 result_size = bind.c_size_t()
121
122 result_buff = bind.lib.nyx_base64_decode(ctypes.byref(result_size), len(data), data)
123
124 return bind.take_bytes(result_buff, result_size.value)
125
126########################################################################################################################
127
128__all__ = [name for name in globals() if name.lower().startswith('nyx')]
129
130########################################################################################################################
uint32_t nyx_hash(__NYX_ZEROABLE__ size_t size, __NYX_NULLABLE__ BUFF_t buff, uint32_t seed)
Hashes a buffer using the MurmurHash2 algorithm.
__NYX_NULLABLE__ str_t nyx_base64_encode(__NYX_NULLABLE__ size_t *result_len, __NYX_ZEROABLE__ size_t size, __NYX_NULLABLE__ BUFF_t buff)
Encodes a buffer using the Base64 algorithm.
__NYX_NULLABLE__ buff_t nyx_base64_decode(__NYX_NULLABLE__ size_t *result_size, __NYX_ZEROABLE__ size_t len, __NYX_NULLABLE__ STR_t str)
Decodes a string using the Base64 algorithm.
void nyx_generate_mac_addr(uint8_t result_mac[6], uint8_t mac0, uint8_t mac1, STR_t node_id)
Generates a MAC address based on a node identifier.
Definition addr.c:14