PHP 8.5.2
Preview: randbytes.py Size: 3.43 KB
//usr/lib/python3/dist-packages/twisted/python/randbytes.py

# -*- test-case-name: twisted.test.test_randbytes -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Cryptographically secure random implementation, with fallback on normal random.
"""


import os
import random
import warnings

getrandbits = getattr(random, "getrandbits", None)

_fromhex = bytes.fromhex


class SecureRandomNotAvailable(RuntimeError):
    """
    Exception raised when no secure random algorithm is found.
    """


class SourceNotAvailable(RuntimeError):
    """
    Internal exception used when a specific random source is not available.
    """


class RandomFactory:
    """
    Factory providing L{secureRandom} and L{insecureRandom} methods.

    You shouldn't have to instantiate this class, use the module level
    functions instead: it is an implementation detail and could be removed or
    changed arbitrarily.
    """

    # This variable is no longer used, and will eventually be removed.
    randomSources = ()

    getrandbits = getrandbits

    def _osUrandom(self, nbytes: int) -> bytes:
        """
        Wrapper around C{os.urandom} that cleanly manage its absence.
        """
        try:
            return os.urandom(nbytes)
        except (AttributeError, NotImplementedError) as e:
            raise SourceNotAvailable(e)

    def secureRandom(self, nbytes: int, fallback: bool = False) -> bytes:
        """
        Return a number of secure random bytes.

        @param nbytes: number of bytes to generate.
        @type nbytes: C{int}
        @param fallback: Whether the function should fallback on non-secure
            random or not.  Default to C{False}.
        @type fallback: C{bool}

        @return: a string of random bytes.
        @rtype: C{str}
        """
        try:
            return self._osUrandom(nbytes)
        except SourceNotAvailable:
            pass

        if fallback:
            warnings.warn(
                "urandom unavailable - "
                "proceeding with non-cryptographically secure random source",
                category=RuntimeWarning,
                stacklevel=2,
            )
            return self.insecureRandom(nbytes)
        else:
            raise SecureRandomNotAvailable("No secure random source available")

    def _randBits(self, nbytes: int) -> bytes:
        """
        Wrapper around C{os.getrandbits}.
        """
        if self.getrandbits is not None:
            n = self.getrandbits(nbytes * 8)
            hexBytes = ("%%0%dx" % (nbytes * 2)) % n
            return _fromhex(hexBytes)
        raise SourceNotAvailable("random.getrandbits is not available")

    _maketrans = bytes.maketrans
    _BYTES = _maketrans(b"", b"")

    def _randModule(self, nbytes: int) -> bytes:
        """
        Wrapper around the C{random} module.
        """
        return b"".join([bytes([random.choice(self._BYTES)]) for i in range(nbytes)])

    def insecureRandom(self, nbytes: int) -> bytes:
        """
        Return a number of non secure random bytes.

        @param nbytes: number of bytes to generate.
        @type nbytes: C{int}

        @return: a string of random bytes.
        @rtype: C{str}
        """
        try:
            return self._randBits(nbytes)
        except SourceNotAvailable:
            pass
        return self._randModule(nbytes)


factory = RandomFactory()

secureRandom = factory.secureRandom

insecureRandom = factory.insecureRandom

del factory


__all__ = ["secureRandom", "insecureRandom", "SecureRandomNotAvailable"]

Directory Contents

Dirs: 3 × Files: 46

Name Size Perms Modified Actions
test DIR
- drwxr-xr-x 2026-01-08 12:56:22
Edit Download
- drwxr-xr-x 2026-01-08 12:55:41
Edit Download
- drwxr-xr-x 2026-01-08 12:56:23
Edit Download
16.42 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
13.87 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
460 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.96 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
27.49 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
27.43 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
6.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
58.92 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
11.82 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.54 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
7.79 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
21.89 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
9.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
26.21 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.23 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.34 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.43 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
6.96 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
20.02 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.08 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
7.01 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.79 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.62 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.23 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.57 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.45 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.28 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.25 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
10.65 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.34 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
244 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.25 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
33.79 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
26.86 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
273 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.66 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
11.99 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
9.45 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
828 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.42 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.57 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
24.68 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.12 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
228 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
598 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).