Python 密码生成

Python password generation

我需要为我公司的 20 万多个客户生成随机密码。

密码复杂性要求很常见:

  1. length > 8
  2. contains at least one upper case character
  3. contains at least one lower case character
  4. contains at least one number
  5. contains at least one symbols (e.g. @#$%)

这是我用来生成随机密码字符串的 python 3.8 代码,遵循 Google 搜索结果(如 and this)的指南:

import secrets
import string

def getRandomPasswordString(length):
    
    alphabet = string.ascii_letters + string.digits + string.punctuation
    password = ''.join(secrets.choice(alphabet) for i in range(length))

    return password

这在大多数情况下都可以正常工作,但在极少数情况下,生成的密码不符合以下复杂性要求:

=Y[&PE-XXP[//F, (missing lower case letter)

^f~+""uwan]ny)b (missing upper case letter)

AQvoMApuNFyRkJd (missing symbols and numbers)

我知道我可以做这样的事情来确保每种类型的角色都被选中:

import secrets
import string

def getRandomPasswordString(length):
    
    alphabet = string.ascii_letters + string.digits + string.punctuation

    password = secrets.choice(string.ascii_uppercase) + \
            secrets.choice(string.ascii_lowercase) + \
            secrets.choice(string.digits) + \
            secrets.choice(string.punctuation) + \
            ''.join(secrets.choice(alphabet) for i in range(length-4))

    return password

这工作正常,但我不确定在前 4 个字符中强加一些密码模式是否会导致任何问题(即模式是大写 > 小写 > 数字 > 符号)

因此,我想探索是否有任何干净的 one-line/shorter 解决方案来生成所需的密码。

非常感谢

只需在末尾添加以下内容来随机排列生成的密码:

password = "".join(random.sample(password,len(password)))

这样您无需创建模式即可满足要求。

或者您可以打乱需求并编写如下函数:

from random  import sample
from secrets import choice
from string  import *

def getRandomPassword(length):
    alphabet      = ascii_letters + digits + punctuation
    requirements  = [ascii_uppercase,        # at least one uppercase letter
                     ascii_lowercase,        # at least one lowercase letter
                     digits,                 # at least one digit
                     punctuation,            # at least one symbol
                     *(length-4)*[alphabet]] # rest: letters digits and symbols
    return "".join(choice(req) for req in sample(requirements,length)) 
import random
import string

def get_random_string(length):
    letters = string.ascii_letters + string.digits + string.punctuation
    result_str = ''.join(random.choice(letters) for i in range(length))
    print("Random string of length", length, "is:", result_str)

get_random_string(8)
get_random_string(8)
get_random_string(6)
get_random_string(11)
get_random_string(22)

对于任何想要更简单的东西的人,您可以只使用 faker 包。有一种方法 .password() 您可以向其提供参数,使您的密码更复杂或更简单。

显然,安装 fakerpip install faker

然后,只需3行代码:

from faker import Faker
fake = Faker()
print(fake.password())

这将输出长度为 10 的字符串,这些是您可以使用的默认参数:

password(length=10, special_chars=True, digits=True, upper_case=True, lower_case=True)

这只是我终端中的一个简单 运行:

from faker import Faker
fake = Faker()
fake.password()
'S9tBRdbf^C'
fake.password()
'NWN8FB4ku&'
fake.password()
'*0B7Z_TisD'
fake.password()
'L2R_bTkw_^'

如有任何问题,请随时发表评论。

我更愿意让我生成的密码更复杂一些。您可以在 if 语句中更改大写、小写、标点或数字的数量变化:

    import string
    import secrets


    opts = string.ascii_letters + string.digits + string.punctuation
    #Generate a random password with at least two lower case, two upper case, one digit, and two symbols
    while True:
        psswd = ''.join(secrets.choice(opts) for i in range(12))
        if (sum(c.islower() for c in psswd) >=2 and
            sum(c.isupper() for c in psswd) >=2 and
            sum(c in string.punctuation for c in psswd) >=2 and
            any(c.isdigit() for c in psswd) ):
            break
    return psswd