o b @s|dZddlZddlZddlZeeddZejZGddde Z Gddde Z Gdd d Z e Z e jZe jZ[ gd ZdS) zQ Cryptographically secure random implementation, with fallback on normal random. N getrandbitsc@eZdZdZdS)SecureRandomNotAvailablezD Exception raised when no secure random algorithm is found. N__name__ __module__ __qualname____doc__r r :/usr/lib/python3/dist-packages/twisted/python/randbytes.pyrrc@r)SourceNotAvailablezQ Internal exception used when a specific random source is not available. Nrr r r r r r r c@sReZdZdZdZeZddZdddZdd Ze j Z e d d Z d d Z d dZdS) 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 Cs2zt|WSttfy}zt|d}~ww)zO Wrapper around C{os.urandom} that cleanly manage its absence. N)osurandomAttributeErrorNotImplementedErrorr )selfnbyteser r r _osUrandom-s  zRandomFactory._osUrandomFcCsFz||WStyYnw|rtjdtdd||Std)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 available)rr warningswarnRuntimeWarninginsecureRandomr)rrfallbackr r r secureRandom6s   zRandomFactory.secureRandomcCs8|jdur||d}d|d|}t|Std)z3 Wrapper around C{os.getrandbits}. Nz%%0%dxrz#random.getrandbits is not available)r_fromhexr )rrnhexBytesr r r _randBitsSs zRandomFactory._randBitscsdfddt|DS)z6 Wrapper around the C{random} module. r%csg|] }ttjgqSr )bytesrandomchoice_BYTES).0irr r dsz-RandomFactory._randModule..)joinrange)rrr r,r _randModule`szRandomFactory._randModulec Cs4dD]}z t|||WStyYqwdS)z 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} )r$r0N)getattrr )rrsrcr r r rfs  zRandomFactory.insecureRandomN)F)rrrr randomSourcesrrrr$r& maketrans _maketransr)r0rr r r r rs    r)rrr)r rr'rr1rr&fromhexr! RuntimeErrorrr rfactoryrr__all__r r r r s X