rng_base.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #
  2. # Random/OSRNG/rng_base.py : Base class for OSRNG
  3. #
  4. # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  5. #
  6. # ===================================================================
  7. # The contents of this file are dedicated to the public domain. To
  8. # the extent that dedication to the public domain is not available,
  9. # everyone is granted a worldwide, perpetual, royalty-free,
  10. # non-exclusive license to exercise all rights associated with the
  11. # contents of this file for any purpose whatsoever.
  12. # No rights are reserved.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. # ===================================================================
  23. __revision__ = "$Id$"
  24. import sys
  25. if sys.version_info[0] == 2 and sys.version_info[1] == 1:
  26. from Crypto.Util.py21compat import *
  27. class BaseRNG(object):
  28. def __init__(self):
  29. self.closed = False
  30. self._selftest()
  31. def __del__(self):
  32. self.close()
  33. def _selftest(self):
  34. # Test that urandom can return data
  35. data = self.read(16)
  36. if len(data) != 16:
  37. raise AssertionError("read truncated")
  38. # Test that we get different data every time (if we don't, the RNG is
  39. # probably malfunctioning)
  40. data2 = self.read(16)
  41. if data == data2:
  42. raise AssertionError("OS RNG returned duplicate data")
  43. # PEP 343: Support for the "with" statement
  44. def __enter__(self):
  45. pass
  46. def __exit__(self):
  47. """PEP 343 support"""
  48. self.close()
  49. def close(self):
  50. if not self.closed:
  51. self._close()
  52. self.closed = True
  53. def flush(self):
  54. pass
  55. def read(self, N=-1):
  56. """Return N bytes from the RNG."""
  57. if self.closed:
  58. raise ValueError("I/O operation on closed file")
  59. if not isinstance(N, (long, int)):
  60. raise TypeError("an integer is required")
  61. if N < 0:
  62. raise ValueError("cannot read to end of infinite stream")
  63. elif N == 0:
  64. return ""
  65. data = self._read(N)
  66. if len(data) != N:
  67. raise AssertionError("%s produced truncated output (requested %d, got %d)" % (self.name, N, len(data)))
  68. return data
  69. def _close(self):
  70. raise NotImplementedError("child class must implement this")
  71. def _read(self, N):
  72. raise NotImplementedError("child class must implement this")
  73. # vim:set ts=4 sw=4 sts=4 expandtab: