python类SystemRandom()的实例源码

test_random.py 文件源码 项目:privcount 作者: privcount 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
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)
test_counter.py 文件源码 项目:privcount 作者: privcount 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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)
counter.py 文件源码 项目:privcount 作者: privcount 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
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
counter.py 文件源码 项目:privcount 作者: privcount 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
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
host.py 文件源码 项目:charm-heat 作者: openstack 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
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))
host.py 文件源码 项目:charm-heat 作者: openstack 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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))
host.py 文件源码 项目:charm-keystone 作者: openstack 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
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))
host.py 文件源码 项目:charm-keystone 作者: openstack 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
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))
host.py 文件源码 项目:charm-keystone 作者: openstack 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
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))
host.py 文件源码 项目:charm-keystone 作者: openstack 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
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))
host.py 文件源码 项目:charm-keystone 作者: openstack 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
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))
preforkjava.py 文件源码 项目:SoCFoundationFlow 作者: mattaw 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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
prefork.py 文件源码 项目:SoCFoundationFlow 作者: mattaw 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
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
preforkjava.py 文件源码 项目:SoCFoundationFlow 作者: mattaw 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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
prefork.py 文件源码 项目:SoCFoundationFlow 作者: mattaw 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
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
preforkjava.py 文件源码 项目:SoCFoundationFlow 作者: mattaw 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
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
generate_network.py 文件源码 项目:toy_dht 作者: MuxZeroNet 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def nodeName():
    return SystemRandom().choice(list(node_names))
generate_network.py 文件源码 项目:toy_dht 作者: MuxZeroNet 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
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)


问题


面经


文章

微信
公众号

扫码关注公众号