From f3637d20811bc1f24cbd610546d7a01799110790 Mon Sep 17 00:00:00 2001 From: timotheyca Date: Wed, 5 Aug 2020 03:25:44 +0300 Subject: [PATCH] initial commit --- v25/messaging/encoding.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 v25/messaging/encoding.py 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)