utils.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright 2013 Donald Stufft and individual contributors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. from typing import SupportsBytes, Type, TypeVar
  16. import nacl.bindings
  17. from nacl import encoding
  18. _EncryptedMessage = TypeVar("_EncryptedMessage", bound="EncryptedMessage")
  19. class EncryptedMessage(bytes):
  20. """
  21. A bytes subclass that holds a messaged that has been encrypted by a
  22. :class:`SecretBox`.
  23. """
  24. _nonce: bytes
  25. _ciphertext: bytes
  26. @classmethod
  27. def _from_parts(
  28. cls: Type[_EncryptedMessage],
  29. nonce: bytes,
  30. ciphertext: bytes,
  31. combined: bytes,
  32. ) -> _EncryptedMessage:
  33. obj = cls(combined)
  34. obj._nonce = nonce
  35. obj._ciphertext = ciphertext
  36. return obj
  37. @property
  38. def nonce(self) -> bytes:
  39. """
  40. The nonce used during the encryption of the :class:`EncryptedMessage`.
  41. """
  42. return self._nonce
  43. @property
  44. def ciphertext(self) -> bytes:
  45. """
  46. The ciphertext contained within the :class:`EncryptedMessage`.
  47. """
  48. return self._ciphertext
  49. class StringFixer:
  50. def __str__(self: SupportsBytes) -> str:
  51. return str(self.__bytes__())
  52. def bytes_as_string(bytes_in: bytes) -> str:
  53. return bytes_in.decode("ascii")
  54. def random(size: int = 32) -> bytes:
  55. return os.urandom(size)
  56. def randombytes_deterministic(
  57. size: int, seed: bytes, encoder: encoding.Encoder = encoding.RawEncoder
  58. ) -> bytes:
  59. """
  60. Returns ``size`` number of deterministically generated pseudorandom bytes
  61. from a seed
  62. :param size: int
  63. :param seed: bytes
  64. :param encoder: The encoder class used to encode the produced bytes
  65. :rtype: bytes
  66. """
  67. raw_data = nacl.bindings.randombytes_buf_deterministic(size, seed)
  68. return encoder.encode(raw_data)