| 124 | |
| 125 | @staticmethod |
| 126 | def random_string( |
| 127 | length: int = 16, |
| 128 | include_uppercase: bool = True, |
| 129 | include_lowercase: bool = True, |
| 130 | include_digits: bool = True, |
| 131 | include_punctuation: bool = False, |
| 132 | custom_chars: str | None = None, |
| 133 | ) -> str: |
| 134 | if length < 1: |
| 135 | raise ValueError("Length must be at least 1") |
| 136 | |
| 137 | if custom_chars: |
| 138 | chars = custom_chars |
| 139 | else: |
| 140 | chars = "" |
| 141 | if include_uppercase: |
| 142 | chars += string.ascii_uppercase |
| 143 | if include_lowercase: |
| 144 | chars += string.ascii_lowercase |
| 145 | if include_digits: |
| 146 | chars += string.digits |
| 147 | if include_punctuation: |
| 148 | chars += string.punctuation |
| 149 | |
| 150 | if not chars: |
| 151 | raise ValueError("No characters selected for random string generation") |
| 152 | |
| 153 | return "".join(secrets.choice(chars) for _ in range(length)) |