def random_value(modulus):
'''
call python's random.randrange(modulus)
'''
# random.randrange() takes one argument: a maximum value
# and returns a random value in [0, modulus)
# it is *NOT* uniformy distributed in python versions < 3.2
# but this test is not sophisticated enough to pick that up
# https://docs.python.org/3.5/library/random.html#random.randrange
return SystemRandom().randrange(modulus)
python类SystemRandom()的实例源码
def try_adjust_count_signed(modulus):
'''
check that adjust_count_signed works as expected for modulus,
and a randomly chosen number between modulus_min and modulus
'''
# randrange is not uniformly distributed in python versions < 3.2
modulus_random = SystemRandom().randrange(modulus_min, modulus)
check_adjust_count_signed(modulus_random)
check_adjust_count_signed(modulus)
def noise(sigma, sum_of_sq, p_exit):
'''
Sample noise from a gussian distribution
the distribution is over +/- sigma, scaled by the noise weight, which is
calculated from the exit probability p_exit, and the overall sum_of_sq
bandwidth
returns a floating-point value between +sigma and -sigma, scaled by
noise_weight
'''
sigma_i = p_exit * sigma / sqrt(sum_of_sq)
# the noise needs to be cryptographically secure, because knowing the RNG
# state could allow an adversary to remove the noise
random_sample = SystemRandom().gauss(0, sigma_i)
return random_sample
def sample(modulus):
'''
Sample a uniformly distributed value from the SystemRandom CSPRNG
(uses rejection sampling to avoid bias)
returns a long uniformly distributed in [0, modulus)
'''
# sanitise input
modulus = long(modulus)
assert modulus > 0
# to get values up to modulus-1, we need this many bits
sample_bit_count = (modulus-1).bit_length()
# handle the case where modulus is 1
if sample_bit_count == 0:
sample_bit_count = 1
# check the bit count is sane
assert modulus <= 2L**sample_bit_count
assert modulus >= 2L**(sample_bit_count-1)
## Unbiased sampling through rejection sampling
while True:
# sample that many bits
v = SystemRandom().getrandbits(sample_bit_count)
assert v >= 0
assert v < 2L**sample_bit_count
# the maximum rejection rate is 1 in 2, when modulus is 2**N + 1
if 0L <= v < modulus:
break
return v
def pwgen(length=None):
"""Generate a random pasword."""
if length is None:
# A random length is ok to use a weak PRNG
length = random.choice(range(35, 45))
alphanumeric_chars = [
l for l in (string.ascii_letters + string.digits)
if l not in 'l0QD1vAEIOUaeiou']
# Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the
# actual password
random_generator = random.SystemRandom()
random_chars = [
random_generator.choice(alphanumeric_chars) for _ in range(length)]
return(''.join(random_chars))
def pwgen(length=None):
"""Generate a random pasword."""
if length is None:
# A random length is ok to use a weak PRNG
length = random.choice(range(35, 45))
alphanumeric_chars = [
l for l in (string.ascii_letters + string.digits)
if l not in 'l0QD1vAEIOUaeiou']
# Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the
# actual password
random_generator = random.SystemRandom()
random_chars = [
random_generator.choice(alphanumeric_chars) for _ in range(length)]
return(''.join(random_chars))
def pwgen(length=None):
"""Generate a random pasword."""
if length is None:
# A random length is ok to use a weak PRNG
length = random.choice(range(35, 45))
alphanumeric_chars = [
l for l in (string.ascii_letters + string.digits)
if l not in 'l0QD1vAEIOUaeiou']
# Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the
# actual password
random_generator = random.SystemRandom()
random_chars = [
random_generator.choice(alphanumeric_chars) for _ in range(length)]
return(''.join(random_chars))
def pwgen(length=None):
"""Generate a random pasword."""
if length is None:
# A random length is ok to use a weak PRNG
length = random.choice(range(35, 45))
alphanumeric_chars = [
l for l in (string.ascii_letters + string.digits)
if l not in 'l0QD1vAEIOUaeiou']
# Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the
# actual password
random_generator = random.SystemRandom()
random_chars = [
random_generator.choice(alphanumeric_chars) for _ in range(length)]
return(''.join(random_chars))
def pwgen(length=None):
"""Generate a random pasword."""
if length is None:
# A random length is ok to use a weak PRNG
length = random.choice(range(35, 45))
alphanumeric_chars = [
l for l in (string.ascii_letters + string.digits)
if l not in 'l0QD1vAEIOUaeiou']
# Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the
# actual password
random_generator = random.SystemRandom()
random_chars = [
random_generator.choice(alphanumeric_chars) for _ in range(length)]
return(''.join(random_chars))
def pwgen(length=None):
"""Generate a random pasword."""
if length is None:
# A random length is ok to use a weak PRNG
length = random.choice(range(35, 45))
alphanumeric_chars = [
l for l in (string.ascii_letters + string.digits)
if l not in 'l0QD1vAEIOUaeiou']
# Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the
# actual password
random_generator = random.SystemRandom()
random_chars = [
random_generator.choice(alphanumeric_chars) for _ in range(length)]
return(''.join(random_chars))
def pwgen(length=None):
"""Generate a random pasword."""
if length is None:
# A random length is ok to use a weak PRNG
length = random.choice(range(35, 45))
alphanumeric_chars = [
l for l in (string.ascii_letters + string.digits)
if l not in 'l0QD1vAEIOUaeiou']
# Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the
# actual password
random_generator = random.SystemRandom()
random_chars = [
random_generator.choice(alphanumeric_chars) for _ in range(length)]
return(''.join(random_chars))
def init_key(ctx):
try:
key = ctx.SHARED_KEY = os.environ['SHARED_KEY']
except KeyError:
key = "".join([chr(random.SystemRandom().randint(40, 126)) for x in range(20)])
os.environ['SHARED_KEY'] = ctx.SHARED_KEY = key
os.environ['PREFORKPID'] = str(os.getpid())
return key
def init_key(ctx):
try:
key = ctx.SHARED_KEY = os.environ['SHARED_KEY']
except KeyError:
key = "".join([chr(random.SystemRandom().randint(40, 126)) for x in range(20)])
os.environ['SHARED_KEY'] = ctx.SHARED_KEY = key
os.environ['PREFORKPID'] = str(os.getpid())
return key
def init_key(ctx):
try:
key = ctx.SHARED_KEY = os.environ['SHARED_KEY']
except KeyError:
key = "".join([chr(random.SystemRandom().randint(40, 126)) for x in range(20)])
os.environ['SHARED_KEY'] = ctx.SHARED_KEY = key
os.environ['PREFORKPID'] = str(os.getpid())
return key
def init_key(ctx):
try:
key = ctx.SHARED_KEY = os.environ['SHARED_KEY']
except KeyError:
key = "".join([chr(random.SystemRandom().randint(40, 126)) for x in range(20)])
os.environ['SHARED_KEY'] = ctx.SHARED_KEY = key
os.environ['PREFORKPID'] = str(os.getpid())
return key
def init_key(ctx):
try:
key = ctx.SHARED_KEY = os.environ['SHARED_KEY']
except KeyError:
key = "".join([chr(random.SystemRandom().randint(40, 126)) for x in range(20)])
os.environ['SHARED_KEY'] = ctx.SHARED_KEY = key
os.environ['PREFORKPID'] = str(os.getpid())
return key
def nodeName():
return SystemRandom().choice(list(node_names))
def makeNames(n):
for node in BOOTSTRAP:
node_names.add(node)
for i in range(n):
name = SystemRandom().choice(LETTERS) + SystemRandom().choice(LETTERS)
node_names.add(name)