OLD | NEW |
(Empty) | |
| 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 |
| 24 __revision__ = "$Id$" |
| 25 |
| 26 import sys |
| 27 if sys.version_info[0] == 2 and sys.version_info[1] == 1: |
| 28 from Crypto.Util.py21compat import * |
| 29 |
| 30 class BaseRNG(object): |
| 31 |
| 32 def __init__(self): |
| 33 self.closed = False |
| 34 self._selftest() |
| 35 |
| 36 def __del__(self): |
| 37 self.close() |
| 38 |
| 39 def _selftest(self): |
| 40 # Test that urandom can return data |
| 41 data = self.read(16) |
| 42 if len(data) != 16: |
| 43 raise AssertionError("read truncated") |
| 44 |
| 45 # Test that we get different data every time (if we don't, the RNG is |
| 46 # probably malfunctioning) |
| 47 data2 = self.read(16) |
| 48 if data == data2: |
| 49 raise AssertionError("OS RNG returned duplicate data") |
| 50 |
| 51 # PEP 343: Support for the "with" statement |
| 52 def __enter__(self): |
| 53 pass |
| 54 def __exit__(self): |
| 55 """PEP 343 support""" |
| 56 self.close() |
| 57 |
| 58 def close(self): |
| 59 if not self.closed: |
| 60 self._close() |
| 61 self.closed = True |
| 62 |
| 63 def flush(self): |
| 64 pass |
| 65 |
| 66 def read(self, N=-1): |
| 67 """Return N bytes from the RNG.""" |
| 68 if self.closed: |
| 69 raise ValueError("I/O operation on closed file") |
| 70 if not isinstance(N, (long, int)): |
| 71 raise TypeError("an integer is required") |
| 72 if N < 0: |
| 73 raise ValueError("cannot read to end of infinite stream") |
| 74 elif N == 0: |
| 75 return "" |
| 76 data = self._read(N) |
| 77 if len(data) != N: |
| 78 raise AssertionError("%s produced truncated output (requested %d, go
t %d)" % (self.name, N, len(data))) |
| 79 return data |
| 80 |
| 81 def _close(self): |
| 82 raise NotImplementedError("child class must implement this") |
| 83 |
| 84 def _read(self, N): |
| 85 raise NotImplementedError("child class must implement this") |
| 86 |
| 87 |
| 88 # vim:set ts=4 sw=4 sts=4 expandtab: |
OLD | NEW |