1 min read

Python 랜덤 패스워드 생성

Parameter 설명

passwordLength: 패스워드 길이
digitLength: 최소 숫자 개수
asciiLettersLength: 최소 대소문자 개수(구분X)

특수문자는 1개 이상으로 설정

import string
import random

def CookPassword(passwordLength, digitLength, asciiLettersLength):

    signRole = '_-|@.,?/!˜#$%ˆ&*(){}[]='

    signForUse = string.ascii_letters + string.digits + signRole
    
    createPassword = ""
    createPassword += random.choice(signForUse)

    while True:
        punctuationCount = 0
        asciiCount = 0
        digitsCount = 0
        if len(createPassword) == passwordLength:
            for punctuation in range(len(createPassword)):
                if createPassword[punctuation] in signRole:
                    punctuationCount += 1
            for asciis in range(len(createPassword)):
                if str(createPassword[asciis]) in string.digits:
                    digitsCount += 1
            for digits in range(len(createPassword)):
                if createPassword[digits] in string.ascii_letters:
                    asciiCount += 1
            if asciiCount >= asciiLettersLength and punctuationCount > 0 and digitsCount >= digitLength:
                break
            else: createPassword = ""
        else:
            createPassword += random.choice(signForUse)

    return createPassword

간단 사용법

# 비밀번호 길이: 20자
# 숫자 개수: 1개 이상
# 대소문자 개수: 4개 이상
newPassword = CookPassword(20,1,4)
print(newPassword)