← Back
Editing: randbytes.cpython-38.pyc
U W[w � @ s� d Z ddlmZmZ ddlZddlZddlZddlZddlm Z e edd�Ze rXej Zndd� ZG dd � d e�ZG d d� de�ZG dd � d e�Ze� ZejZejZ[ddd gZdS )zQ Cryptographically secure random implementation, with fallback on normal random. � )�division�absolute_importN)�_PY3�getrandbitsc C s | � d�S )N�hex)�decode)�hexBytes� r �:/usr/lib/python3/dist-packages/twisted/python/randbytes.py�_fromhex s r c @ s e Zd ZdZdS )�SecureRandomNotAvailablezD Exception raised when no secure random algorithm is found. N��__name__� __module__�__qualname__�__doc__r r r r r s r c @ s e Zd ZdZdS )�SourceNotAvailablezQ Internal exception used when a specific random source is not available. Nr r r r r r s r c @ sf e Zd ZdZdZeZdd� Zddd�Zdd � Ze rBe jZd d� Z nejZdd� Z ed d �Zdd� ZdS )� RandomFactoryz� 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. r c C s@ zt �|�W S ttfk r: } zt|��W 5 d}~X Y nX dS )zO Wrapper around C{os.urandom} that cleanly manage its absence. N)�os�urandom�AttributeError�NotImplementedErrorr )�self�nbytes�er r r � _osUrandom5 s zRandomFactory._osUrandomFc C sL z| � |�W S tk r Y nX |r@tjdtdd� | �|�S td��dS )ak 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} zPurandom unavailable - proceeding with non-cryptographically secure random source� )�category� stacklevelz!No secure random source availableN)r r �warnings�warn�RuntimeWarning�insecureRandomr )r r Zfallbackr r r �secureRandom? s � zRandomFactory.secureRandomc C s<