Skip to content

Commit

Permalink
refactor: implement better random string generator
Browse files Browse the repository at this point in the history
  • Loading branch information
rpoisel committed Jan 17, 2025
1 parent ada38cc commit 41507c3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 6 additions & 0 deletions util/crypto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import secrets
import string


def generate_random_string(k: int) -> str:
return "".join(secrets.choice(string.ascii_letters + string.digits) for _ in range(k))
6 changes: 2 additions & 4 deletions util/fs.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import logging
import random
import string
import tempfile
from pathlib import Path

from crypto import generate_random_string
from labgrid.driver import ShellDriver, SSHDriver
from process import run


def create_temp_dir() -> Path:
while True:
# we don't need a super-secure cryptographic PRNG
random_name = "".join(random.choices(string.ascii_letters + string.digits, k=12)) # noqa: S311
random_name = generate_random_string(12)
temp_path = Path(tempfile.gettempdir()) / random_name
logging.info(f"Creating temporary directory {temp_path}.")
try:
Expand Down

0 comments on commit 41507c3

Please sign in to comment.