perf: Random secret string

pull/14622/head
feng 2024-12-09 15:18:14 +08:00 committed by ZhaoJiSen
parent 1191e4ab2d
commit 3796af78a6
1 changed files with 36 additions and 16 deletions

View File

@ -18,9 +18,8 @@ def random_ip():
return socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff))) return socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff)))
def random_replace_char(s, chars, length): def random_replace_char(seq, chars, length):
using_index = set() using_index = set()
seq = list(s)
while length > 0: while length > 0:
index = secrets.randbelow(len(seq) - 1) index = secrets.randbelow(len(seq) - 1)
@ -29,7 +28,7 @@ def random_replace_char(s, chars, length):
seq[index] = secrets.choice(chars) seq[index] = secrets.choice(chars)
using_index.add(index) using_index.add(index)
length -= 1 length -= 1
return ''.join(seq) return seq
def remove_exclude_char(s, exclude_chars): def remove_exclude_char(s, exclude_chars):
@ -47,19 +46,40 @@ def random_string(
if length < 4: if length < 4:
raise ValueError('The length of the string must be greater than 3') raise ValueError('The length of the string must be greater than 3')
chars_map = ( char_list = []
(lower, string.ascii_lowercase), if lower:
(upper, string.ascii_uppercase),
(digit, string.digits), lower_chars = remove_exclude_char(string.ascii_lowercase, exclude_chars)
) if not lower_chars:
chars = ''.join([i[1] for i in chars_map if i[0]]) raise ValueError('After excluding characters, no lowercase letters are available.')
chars = remove_exclude_char(chars, exclude_chars) char_list.append(lower_chars)
texts = list(secrets.choice(chars) for __ in range(length))
texts = ''.join(texts) if upper:
upper_chars = remove_exclude_char(string.ascii_uppercase, exclude_chars)
if not upper_chars:
raise ValueError('After excluding characters, no uppercase letters are available.')
char_list.append(upper_chars)
if digit:
digit_chars = remove_exclude_char(string.digits, exclude_chars)
if not digit_chars:
raise ValueError('After excluding characters, no digits are available.')
char_list.append(digit_chars)
secret_chars = [secrets.choice(chars) for chars in char_list]
all_chars = ''.join(char_list)
remaining_length = length - len(secret_chars)
seq = [secrets.choice(all_chars) for _ in range(remaining_length)]
# 控制一下特殊字符的数量, 别随机出来太多
if special_char: if special_char:
symbols = remove_exclude_char(symbols, exclude_chars) special_chars = remove_exclude_char(symbols, exclude_chars)
if not special_chars:
raise ValueError('After excluding characters, no special characters are available.')
symbol_num = length // 16 + 1 symbol_num = length // 16 + 1
texts = random_replace_char(texts, symbols, symbol_num) seq = random_replace_char(seq, symbols, symbol_num)
return texts secret_chars += seq
secrets.SystemRandom().shuffle(secret_chars)
return ''.join(secret_chars)