diff --git a/v25/messaging/encoding.py b/v25/messaging/encoding.py new file mode 100644 index 0000000..0c31231 --- /dev/null +++ b/v25/messaging/encoding.py @@ -0,0 +1,21 @@ +from typing import Any, Optional + +import nacl.encoding +import nacl.utils + +ENCODER = nacl.encoding.URLSafeBase64Encoder +NONCE_SIZE = 32 + + +class Encoding: + @staticmethod + def encode(raw: Any) -> Optional[str]: + return None if raw is None else ENCODER.encode(bytes(raw)).decode('ascii') + + @staticmethod + def decode(s: str) -> bytes: + return None if s is None else ENCODER.decode(s.encode('ascii')) + + @classmethod + def nonce(cls): + return nacl.utils.random(NONCE_SIZE)