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