Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/Crypto/Random/OSRNG/rng_base.py: 64%
44 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 07:03 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 07:03 +0000
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# ===================================================================
24__revision__ = "$Id$"
26import sys
27if sys.version_info[0] == 2 and sys.version_info[1] == 1:
28 from Crypto.Util.py21compat import *
30class BaseRNG(object):
32 def __init__(self):
33 self.closed = False
34 self._selftest()
36 def __del__(self):
37 self.close()
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")
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")
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()
58 def close(self):
59 if not self.closed:
60 self._close()
61 self.closed = True
63 def flush(self):
64 pass
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, 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, got %d)" % (self.name, N, len(data)))
79 return data
81 def _close(self):
82 raise NotImplementedError("child class must implement this")
84 def _read(self, N):
85 raise NotImplementedError("child class must implement this")
88# vim:set ts=4 sw=4 sts=4 expandtab: