__init__.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Random/__init__.py : PyCryptodome random number generation
  4. #
  5. # ===================================================================
  6. # The contents of this file are dedicated to the public domain. To
  7. # the extent that dedication to the public domain is not available,
  8. # everyone is granted a worldwide, perpetual, royalty-free,
  9. # non-exclusive license to exercise all rights associated with the
  10. # contents of this file for any purpose whatsoever.
  11. # No rights are reserved.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. # ===================================================================
  22. __all__ = ['new', 'get_random_bytes']
  23. from os import urandom
  24. class _UrandomRNG(object):
  25. def read(self, n):
  26. """Return a random byte string of the desired size."""
  27. return urandom(n)
  28. def flush(self):
  29. """Method provided for backward compatibility only."""
  30. pass
  31. def reinit(self):
  32. """Method provided for backward compatibility only."""
  33. pass
  34. def close(self):
  35. """Method provided for backward compatibility only."""
  36. pass
  37. def new(*args, **kwargs):
  38. """Return a file-like object that outputs cryptographically random bytes."""
  39. return _UrandomRNG()
  40. def atfork():
  41. pass
  42. #: Function that returns a random byte string of the desired size.
  43. get_random_bytes = urandom