Skip to content

Commit

Permalink
perf: Random secret string
Browse files Browse the repository at this point in the history
  • Loading branch information
feng626 authored and ZhaoJiSen committed Dec 9, 2024
1 parent 1191e4a commit 3796af7
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions apps/common/utils/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ def random_ip():
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()
seq = list(s)

while length > 0:
index = secrets.randbelow(len(seq) - 1)
Expand All @@ -29,7 +28,7 @@ def random_replace_char(s, chars, length):
seq[index] = secrets.choice(chars)
using_index.add(index)
length -= 1
return ''.join(seq)
return seq


def remove_exclude_char(s, exclude_chars):
Expand All @@ -47,19 +46,40 @@ def random_string(
if length < 4:
raise ValueError('The length of the string must be greater than 3')

chars_map = (
(lower, string.ascii_lowercase),
(upper, string.ascii_uppercase),
(digit, string.digits),
)
chars = ''.join([i[1] for i in chars_map if i[0]])
chars = remove_exclude_char(chars, exclude_chars)
texts = list(secrets.choice(chars) for __ in range(length))
texts = ''.join(texts)

# 控制一下特殊字符的数量, 别随机出来太多
char_list = []
if lower:

lower_chars = remove_exclude_char(string.ascii_lowercase, exclude_chars)
if not lower_chars:
raise ValueError('After excluding characters, no lowercase letters are available.')
char_list.append(lower_chars)

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:
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
texts = random_replace_char(texts, symbols, symbol_num)
return texts
seq = random_replace_char(seq, symbols, symbol_num)
secret_chars += seq

secrets.SystemRandom().shuffle(secret_chars)
return ''.join(secret_chars)

0 comments on commit 3796af7

Please sign in to comment.