使用 python 生成 8 个字符的密码,包括大小写和数字
generating a 8 character password including lower case and upper case and numbers using python
我想使用python生成包括大小写和数字的密码,但它应该保证这3种都被使用过。到目前为止,我写了这个,但它不能保证所有 3 种字符都被使用。我想将 8 个字符分成 2 部分。前 3 个和后 5 个。然后确保在第一部分中使用了所有 3 种字符,然后将它们与下一部分打乱,但我不知道如何编码。
import random
a = 0
password = ''
while a < 8:
if random.randint(0, 61) < 10:
password += chr(random.randint(48, 57))
elif 10<random.randint(0, 61)<36:
password += chr(random.randint(65, 90))
else:
password += chr(random.randint(97, 122))
a += 1
print(password)
您的问题由三部分组成:
- 将 8 个字符分成 2 部分 - 前 3 和后 5 -(字符串切片)
- 确保在第一部分中使用了所有 3 种字符(验证密码)
- 打乱字符(打乱字符串)
第 1 部分: 分割字符串
这是一个很好的教程,教您如何 slice strings using python..
在你的情况下,如果你在代码末尾插入这个...
print(password[:3])
print(password[3:])
...您将看到前 3 个字符和后 5 个字符。
第 2 部分: 验证密码
可以找到一个好的答案 here。
def password_check(password):
# calculating the length
length_error = len(password) < 8
# searching for digits
digit_error = re.search(r"\d", password) is None
# searching for uppercase
uppercase_error = re.search(r"[A-Z]", password) is None
# searching for lowercase
lowercase_error = re.search(r"[a-z]", password) is None
# overall result
password_ok = not ( length_error or digit_error or uppercase_error or lowercase_error)
return password_ok
password_check(password)
如果满足所有条件,此函数将 return True
,否则 False
。
第 3 部分: 打乱字符串
if password_check(password) == True:
new_pwd = ''.join(random.sample(password,len(password)))
print new_pwd
此代码将打乱整个密码并将其分配给名为 new_pwd
的新变量
ps.
整个代码可以找到here!
如你所说:前三个位置分别从大小写字符和数字中随机选择,其余从所有个字符中随机选择,然后洗牌。
from random import choice, shuffle
from string import ascii_uppercase, ascii_lowercase, digits
pwd = [choice(ascii_lowercase), choice(ascii_uppercase), choice(digits)] \
+ [choice(ascii_lowercase + ascii_uppercase + digits) for _ in range(5)]
shuffle(pwd)
pwd = ''.join(pwd)
请注意,这种方式的数字可能有点 'underrepresented',因为它们是从所有可用字符中随机选择的,因此前三个字符之后的任何字符都有 10/62 的几率将是一个数字。如果你想让 1/3 的字符是数字(这可能不是一个好主意——见下文),你可以首先随机选择一个字符组,每个字符组有 1/3 的机会,然后从该组中选择一个字符:
pwd = [choice(ascii_lowercase), choice(ascii_uppercase), choice(digits)] \
+ [choice(choice([ascii_lowercase, ascii_uppercase, digits])) for _ in range(5)]
但请注意,这将 降低 随机性,从而降低密码的安全性 - 但三组中至少有一组的要求也是如此。
您可以使用正则表达式并检查是否有任何字符与正则表达式匹配。
import re
string = "AaBbCc12"
if re.search('[a-z]',string)\
and re.search('[0-9]',string)\
and re.search('[A-Z]',string):
print("This is a string you are looking for")
else:
print("Nope")
这是一个不太优雅的解决方案,但在理解和适应性方面是最快的。
我想使用python生成包括大小写和数字的密码,但它应该保证这3种都被使用过。到目前为止,我写了这个,但它不能保证所有 3 种字符都被使用。我想将 8 个字符分成 2 部分。前 3 个和后 5 个。然后确保在第一部分中使用了所有 3 种字符,然后将它们与下一部分打乱,但我不知道如何编码。
import random
a = 0
password = ''
while a < 8:
if random.randint(0, 61) < 10:
password += chr(random.randint(48, 57))
elif 10<random.randint(0, 61)<36:
password += chr(random.randint(65, 90))
else:
password += chr(random.randint(97, 122))
a += 1
print(password)
您的问题由三部分组成:
- 将 8 个字符分成 2 部分 - 前 3 和后 5 -(字符串切片)
- 确保在第一部分中使用了所有 3 种字符(验证密码)
- 打乱字符(打乱字符串)
第 1 部分: 分割字符串
这是一个很好的教程,教您如何 slice strings using python..
在你的情况下,如果你在代码末尾插入这个...
print(password[:3])
print(password[3:])
...您将看到前 3 个字符和后 5 个字符。
第 2 部分: 验证密码
可以找到一个好的答案 here。
def password_check(password):
# calculating the length
length_error = len(password) < 8
# searching for digits
digit_error = re.search(r"\d", password) is None
# searching for uppercase
uppercase_error = re.search(r"[A-Z]", password) is None
# searching for lowercase
lowercase_error = re.search(r"[a-z]", password) is None
# overall result
password_ok = not ( length_error or digit_error or uppercase_error or lowercase_error)
return password_ok
password_check(password)
如果满足所有条件,此函数将 return True
,否则 False
。
第 3 部分: 打乱字符串
if password_check(password) == True:
new_pwd = ''.join(random.sample(password,len(password)))
print new_pwd
此代码将打乱整个密码并将其分配给名为 new_pwd
ps. 整个代码可以找到here!
如你所说:前三个位置分别从大小写字符和数字中随机选择,其余从所有个字符中随机选择,然后洗牌。
from random import choice, shuffle
from string import ascii_uppercase, ascii_lowercase, digits
pwd = [choice(ascii_lowercase), choice(ascii_uppercase), choice(digits)] \
+ [choice(ascii_lowercase + ascii_uppercase + digits) for _ in range(5)]
shuffle(pwd)
pwd = ''.join(pwd)
请注意,这种方式的数字可能有点 'underrepresented',因为它们是从所有可用字符中随机选择的,因此前三个字符之后的任何字符都有 10/62 的几率将是一个数字。如果你想让 1/3 的字符是数字(这可能不是一个好主意——见下文),你可以首先随机选择一个字符组,每个字符组有 1/3 的机会,然后从该组中选择一个字符:
pwd = [choice(ascii_lowercase), choice(ascii_uppercase), choice(digits)] \
+ [choice(choice([ascii_lowercase, ascii_uppercase, digits])) for _ in range(5)]
但请注意,这将 降低 随机性,从而降低密码的安全性 - 但三组中至少有一组的要求也是如此。
您可以使用正则表达式并检查是否有任何字符与正则表达式匹配。
import re
string = "AaBbCc12"
if re.search('[a-z]',string)\
and re.search('[0-9]',string)\
and re.search('[A-Z]',string):
print("This is a string you are looking for")
else:
print("Nope")
这是一个不太优雅的解决方案,但在理解和适应性方面是最快的。