我有以下方法来生成随 secret 码:
char_set = {
'small': 'abcdefghijklmnopqrstuvwxyz',
'nums': '0123456789',
'big': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'special': '^!\$%&/()=?{[]}+~#-_.:,;<>|\\'
}
def gen_password(length=21):
'''
Generate a random password containing upper and lower case letters,
numbers and special characters.
Arguments:
----------
The default length is 21 chars, however the minimum is 8 chars.
Returns:
--------
str: The random password
'''
assert length >= 8
password = []
while len(password) < length:
key = random.choice(char_set.keys())
a_char = os.urandom(1)
if a_char in char_set[key]:
if check_prev_char(password, char_set[key]):
continue
else:
password.append(a_char)
return ''.join(password)
def check_prev_char(password, current_char_set):
'''
Ensure that there are not consecutive Uppercase / Lowecase / Number / special
characters in the password.
Arguments:
----------
password: (str)
The candidate password
current_char_set: (char)
The current character to be compared.
Returns:
--------
Bool: if the candidate password is good so far.
'''
index = len(password)
if index == 0:
return False
else:
prev_char = password[index - 1]
if prev_char in current_char_set:
return True
else:
return False
尽管它在大多数情况下都有效,但也有少数情况会失败,例如,当密码包含 \
或不包含数字时。
如何确保我生成的所有密码不包含 \
并且始终至少包含一个数字?
最佳答案
如果您不想在密码中包含 \
,请将它们从特殊字符集中删除(有它存在的原因吗?):
'special': '^!\$%&/()=?{[]}+~#-_.:,;<>|'
为了确保它始终有一个数字,请生成一个包含 n-1
个字符的密码,然后在随机位置添加一个数字:
def gen_final_password(length=21):
password = gen_password(length - 1)
index = random.randint(0, length)
number = random.choice(char_set['nums'])
return password[:index] + number + password[index:]
但请注意,这可能不会生成具有完整预期熵的加密随 secret 码。为此,您需要始终使用加密随机源并以相同的可能性使用所有字符。目前,该算法更喜欢小字符集中的字符(例如,它更有可能包含 0
而不是 a
)。此外,避免使用同一字符集的连续字符会对熵产生未知的不良影响。
最后,check_prev_char
可以简化为表达式:
len(password) and password[-1] in char_set[key]
关于python - 生成有效密码 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33400639/