3 M(YS @sLdZddlZddlmZmZddlmZddZddZd d Z d d Z dS) z(Functions for generating random numbers.N)common transform)bytecCsJt|d\}}tj|}|dkrFttjd}|d|L}t||}|S)zReads 'nbits' random bits. If nbits isn't a whole number of bytes, an extra byte will be appended with only the lower bits set. r)divmodosurandomordr)nbitsnbytesZrbits randomdataZ randomvaluer2/private/tmp/pip-build-nl73fm5q/rsa/rsa/randnum.pyread_random_bitss   rcCs&t|}tj|}|d|d>O}|S)z8Reads a random integer of approximately nbits bits. r)rrZ bytes2int)r r valuerrrread_random_int0s rcCst|}|dBS)zhReads a random odd integer of approximately nbits bits. >>> read_random_odd_int(512) & 1 1 r)r)r rrrrread_random_odd_int>srcCsJtj|}d}x6t|}||kr"P|r:|ddkr:|d8}|d7}qW|S)zReturns a random integer x with 1 <= x <= maxvalue May take a very long time in specific situations. If maxvalue needs N bits to store, the closer maxvalue is to (2 ** N) - 1, the faster this function is. r r)rbit_sizer)maxvaluertriesrrrrrandintKs  r) __doc__rZrsarrZ rsa._compatrrrrrrrrrs