mirror of
https://github.com/jumpserver/jumpserver.git
synced 2026-05-07 04:32:11 +00:00
perf: 随机密码生成规则添加可排除字符选项
This commit is contained in:
@@ -32,7 +32,16 @@ def random_replace_char(s, chars, length):
|
||||
return ''.join(seq)
|
||||
|
||||
|
||||
def random_string(length: int, lower=True, upper=True, digit=True, special_char=False, symbols=string_punctuation):
|
||||
def remove_exclude_char(s, exclude_chars):
|
||||
for i in exclude_chars:
|
||||
s = s.replace(i, '')
|
||||
return s
|
||||
|
||||
|
||||
def random_string(
|
||||
length: int, lower=True, upper=True, digit=True,
|
||||
special_char=False, exclude_chars='', symbols=string_punctuation
|
||||
):
|
||||
if not any([lower, upper, digit]):
|
||||
raise ValueError('At least one of `lower`, `upper`, `digit` must be `True`')
|
||||
if length < 4:
|
||||
@@ -44,11 +53,13 @@ def random_string(length: int, lower=True, upper=True, digit=True, special_char=
|
||||
(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)
|
||||
|
||||
# 控制一下特殊字符的数量, 别随机出来太多
|
||||
if special_char:
|
||||
symbols = remove_exclude_char(symbols, exclude_chars)
|
||||
symbol_num = length // 16 + 1
|
||||
texts = random_replace_char(texts, symbols, symbol_num)
|
||||
return texts
|
||||
|
||||
Reference in New Issue
Block a user