Ϫf dZddlZddlZddlZeeddZejZGdde Z Gdde Z Gdd Z e Z e jZe jZ[ gd Zy) zQ Cryptographically secure random implementation, with fallback on normal random. N getrandbitsceZdZdZy)SecureRandomNotAvailablezD Exception raised when no secure random algorithm is found. N__name__ __module__ __qualname____doc__:/usr/lib/python3/dist-packages/twisted/python/randbytes.pyrrr rceZdZdZy)SourceNotAvailablezQ Internal exception used when a specific random source is not available. Nrr r r rrrr rceZdZdZdZeZdedefdZd dede defdZ dedefdZ ejZ e d d Zdedefd Zdedefd Zy ) 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 nbytesreturncr tj|S#ttf$r}t |d}~wwxYw)zO Wrapper around C{os.urandom} that cleanly manage its absence. N)osurandomAttributeErrorNotImplementedErrorr)selfres r _osUrandomzRandomFactory._osUrandom-s7 (::f% % 34 ($Q' ' (s6 16fallbackc |j|S#t$rYnwxYw|r-tjdtd|j |St d)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)rrwarningswarnRuntimeWarninginsecureRandomr)rrrs r secureRandomzRandomFactory.secureRandom6sd ??6* *!     MMM'   &&v. .*+NO O  c|j*|j|dz}d|dzz|z}t|Std)z3 Wrapper around C{os.getrandbits}. z%%0%dxrz#random.getrandbits is not available)r_fromhexr)rrnhexBytess r _randBitszRandomFactory._randBitsSsN    '  !,A FQJ/14HH% % !FGGr r c djt|Dcgc]+}ttj|j g-c}Scc}w)z6 Wrapper around the C{random} module. r )joinrangebytesrandomchoice_BYTES)rris r _randModulezRandomFactory._randModule`s:xxeFmT dkk :;<TUUTs0Acf |j|S#t$rYnwxYw|j|S)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-rr6)rrs r r%zRandomFactory.insecureRandomfs9 >>&) )!   ''r'N)F)rrr r randomSourcesrintr1rboolr&r- maketrans _maketransr4r6r%r r r rrsMK(((P3P$P5P:HHHJ S !FV#V%V (S(U(r r)r&r%r)r rr2r"getattrrr1fromhexr* RuntimeErrorrrrfactoryr&r%__all__r r r rBs{   fmT2 ==|  U(U(p /## ''  Ir