Fix a couple of problems with random hex string generation
get_random_string uses random.random(), which generates only 53bits of randomness, but it then calculates 224bits wide hash of the value. Simply use random.getrandbits() instead. Convert get_rand_name's hex string generation logic to use get_random_string, too. Also remove a call of random.seed with microsecond as its argument, which degrades random number quality. The RNG has been initialized with os.urandom() with enough bits of randomness. Change-Id: I9302c501b19aa7bf448d95b8f4bdc724c23ed0fb
This commit is contained in:
parent
ccbeda71ac
commit
eccbbd113f
|
@ -19,11 +19,9 @@
|
|||
"""Utilities and helper functions."""
|
||||
|
||||
import collections
|
||||
import datetime
|
||||
import decimal
|
||||
import errno
|
||||
import functools
|
||||
import hashlib
|
||||
import math
|
||||
import multiprocessing
|
||||
import os
|
||||
|
@ -249,17 +247,9 @@ def get_random_mac(base_mac):
|
|||
|
||||
def get_random_string(length):
|
||||
"""Get a random hex string of the specified length.
|
||||
|
||||
based on Cinder library
|
||||
cinder/transfer/api.py
|
||||
"""
|
||||
rndstr = ""
|
||||
random.seed(datetime.datetime.now().microsecond)
|
||||
while len(rndstr) < length:
|
||||
base_str = str(random.random()).encode('utf-8')
|
||||
rndstr += hashlib.sha224(base_str).hexdigest()
|
||||
|
||||
return rndstr[0:length]
|
||||
return "{0:0{1}x}".format(random.getrandbits(length * 4), length)
|
||||
|
||||
|
||||
def get_dhcp_agent_device_id(network_id, host):
|
||||
|
|
|
@ -20,7 +20,6 @@ import contextlib
|
|||
import gc
|
||||
import os
|
||||
import os.path
|
||||
import random
|
||||
import weakref
|
||||
|
||||
import eventlet.timeout
|
||||
|
@ -43,6 +42,7 @@ from neutron.callbacks import registry
|
|||
from neutron.common import config
|
||||
from neutron.common import constants
|
||||
from neutron.common import rpc as n_rpc
|
||||
from neutron.common import utils
|
||||
from neutron.db import agentschedulers_db
|
||||
from neutron import manager
|
||||
from neutron import policy
|
||||
|
@ -84,10 +84,10 @@ def get_rand_name(max_length=None, prefix='test'):
|
|||
if length <= 0:
|
||||
raise ValueError("'max_length' must be bigger than 'len(prefix)'.")
|
||||
|
||||
suffix = ''.join(str(random.randint(0, 9)) for i in range(length))
|
||||
else:
|
||||
suffix = hex(random.randint(0x10000000, 0x7fffffff))[2:]
|
||||
return prefix + suffix
|
||||
length = 8
|
||||
|
||||
return prefix + utils.get_random_string(length)
|
||||
|
||||
|
||||
def get_rand_device_name(prefix='test'):
|
||||
|
|
Loading…
Reference in New Issue