You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.7 KiB

7 years ago
  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. from __future__ import absolute_import, division, print_function
  15. import six
  16. import nacl.bindings
  17. class EncryptedMessage(six.binary_type):
  18. """
  19. A bytes subclass that holds a messaged that has been encrypted by a
  20. :class:`SecretBox`.
  21. """
  22. @classmethod
  23. def _from_parts(cls, nonce, ciphertext, combined):
  24. obj = cls(combined)
  25. obj._nonce = nonce
  26. obj._ciphertext = ciphertext
  27. return obj
  28. @property
  29. def nonce(self):
  30. """
  31. The nonce used during the encryption of the :class:`EncryptedMessage`.
  32. """
  33. return self._nonce
  34. @property
  35. def ciphertext(self):
  36. """
  37. The ciphertext contained within the :class:`EncryptedMessage`.
  38. """
  39. return self._ciphertext
  40. class StringFixer(object):
  41. def __str__(self):
  42. if six.PY3:
  43. return str(self.__bytes__())
  44. else:
  45. return self.__bytes__()
  46. def bytes_as_string(bytes_in):
  47. if six.PY3:
  48. return bytes_in.decode('ascii')
  49. else:
  50. return bytes_in
  51. def random(size=32):
  52. return nacl.bindings.randombytes(size)

Powered by TurnKey Linux.