B dh @shdZddlZddlZddlmZmZeedddZeedddZ eedd d Z eed d d Z dS)z(Functions for generating random numbers.N)common transform)nbitsreturncCsNt|d\}}t|}|dkrJttd}|d|L}td||}|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. rB)divmodosurandomordstructpack)rnbytesZrbits randomdataZ randomvaluer2/tmp/pip-target-jj3kjtpb/lib/python/rsa/randnum.pyread_random_bitss  rcCs&t|}t|}|d|d>O}|S)z8Reads a random integer of approximately nbits bits. r)rrZ bytes2int)rrvaluerrrread_random_int.s rcCst|}|dBS)zhReads a random odd integer of approximately nbits bits. >>> read_random_odd_int(512) & 1 1 r)r)rrrrrread_random_odd_int<sr)maxvaluercCsJt|}d}x6t|}||kr"P|ddkr:|r:|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)rrtriesrrrrrandintIs  r) __doc__r r Zrsarrintbytesrrrrrrrrs