【Python】生成隨機密碼

# -*- coding: utf-8 -*-
# https://docs.python.org/3/library/secrets.html#recipes-and-best-practices

import string, secrets

alphabet = string.ascii_letters + string.digits

def CreatePassword(passwordLen):
	while 1:
		password = ''.join(secrets.choice(alphabet) for i in range(passwordLen))
		if (any(c.islower() for c in password)
				and any(c.isupper() for c in password)
				and any(c.isdigit() for c in password)):
			return password

try:
	passwordLen = int(input('Please input your password length (>= 10, default 20): '))
	if passwordLen < 10:
		passwordLen = 20
except Exception as e:
	print(e)
	passwordLen = 20
finally:
	password = CreatePassword(passwordLen)
	print(password)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章