refactor: protect draw methods not used outside module

This commit is contained in:
Florian Sylvain
2025-03-28 02:00:27 +01:00
parent 6982a5b97e
commit 19f3dc3beb
2 changed files with 36 additions and 36 deletions
+32 -32
View File
@@ -25,23 +25,7 @@ class Draw:
def __init__(self, weather_service):
self.weather_service = weather_service
def get_static_entropy_pool(self):
"""Creates a base entropy pool with sources that don't change rapidly"""
weather_entropy = self.weather_service.get_weather_entropy()
return [
str(os.urandom(32)),
str(secrets.token_bytes(32)),
socket.gethostname(),
str(platform.system_alias(platform.system(),
platform.release(), platform.version())),
str(uuid.getnode()),
str(multiprocessing.cpu_count()),
"".join([str(v) for v in psutil.disk_partitions()]),
str(hash(frozenset(os.environ.items()))),
weather_entropy,
]
def get_dynamic_entropy_pool(self):
def _get_dynamic_entropy_pool(self):
"""Retrieves dynamic data to add entropy"""
return [
str(time.time()),
@@ -59,20 +43,7 @@ class Draw:
str(id({})),
]
def generate_seed(self, base_pool=None):
"""Generates a random seed by combining different entropy sources"""
entropy_sources = base_pool.copy() if base_pool else []
entropy_sources.extend(self.get_dynamic_entropy_pool())
random.shuffle(entropy_sources)
entropy_str = "".join(entropy_sources)
intermediate_hash = hashlib.sha512(entropy_str.encode()).digest()
intermediate_hash = hashlib.blake2b(intermediate_hash).digest()
final_hash = hashlib.sha256(intermediate_hash).hexdigest()
return int(final_hash, 16)
def make_draw(self, base_pool=None):
def _make_draw(self, base_pool=None):
"""Generates a draw with N numbers and M stars using a random seed"""
seed = self.generate_seed(base_pool)
random.seed(seed)
@@ -84,6 +55,35 @@ class Draw:
"stars": sorted(stars)
}
def get_static_entropy_pool(self):
"""Creates a base entropy pool with sources that don't change rapidly"""
weather_entropy = self.weather_service.get_weather_entropy()
return [
str(os.urandom(32)),
str(secrets.token_bytes(32)),
socket.gethostname(),
str(platform.system_alias(platform.system(),
platform.release(), platform.version())),
str(uuid.getnode()),
str(multiprocessing.cpu_count()),
"".join([str(v) for v in psutil.disk_partitions()]),
str(hash(frozenset(os.environ.items()))),
weather_entropy,
]
def generate_seed(self, base_pool=None):
"""Generates a random seed by combining different entropy sources"""
entropy_sources = base_pool.copy() if base_pool else []
entropy_sources.extend(self._get_dynamic_entropy_pool())
random.shuffle(entropy_sources)
entropy_str = "".join(entropy_sources)
intermediate_hash = hashlib.sha512(entropy_str.encode()).digest()
intermediate_hash = hashlib.blake2b(intermediate_hash).digest()
final_hash = hashlib.sha256(intermediate_hash).hexdigest()
return int(final_hash, 16)
def generate_draws(self, num_draws):
"""Generates multiple draws sequentially"""
base_pool = self.get_static_entropy_pool()
@@ -91,7 +91,7 @@ class Draw:
draws = []
for _ in range(num_draws):
draws.append(self.make_draw(base_pool))
draws.append(self._make_draw(base_pool))
time.sleep(0.01)
progress_bar.next()
+4 -4
View File
@@ -29,8 +29,8 @@ class TestDraw(unittest.TestCase):
def test_get_dynamic_entropy_pool(self):
"""Test that dynamic entropy pool returns different values"""
pool1 = self.draw.get_dynamic_entropy_pool()
pool2 = self.draw.get_dynamic_entropy_pool()
pool1 = self.draw._get_dynamic_entropy_pool()
pool2 = self.draw._get_dynamic_entropy_pool()
self.assertIsInstance(pool1, list)
self.assertIsInstance(pool2, list)
@@ -53,7 +53,7 @@ class TestDraw(unittest.TestCase):
[5, 10]
]
draw = self.draw.make_draw(["test_entropy"])
draw = self.draw._make_draw(["test_entropy"])
self.assertIn("seed", draw)
self.assertIn("numbers", draw)
@@ -68,7 +68,7 @@ class TestDraw(unittest.TestCase):
"""Test generation of multiple draws"""
mock_bar_instance = mock_bar.return_value
self.draw.make_draw = MagicMock(
self.draw._make_draw = MagicMock(
side_effect=[{"numbers": [1, 2, 3, 4, 5],
"stars": [1, 2], "seed": i} for i in range(3)]
)