SoFunction
Updated on 2024-11-20

python use regular expression to detect password strength source code sharing


#encoding=utf-8
#-------------------------------------------------------------------------------
# Name: Module 1
# Purpose:
#
# Author:      Administrator
#
# Created:     10-06-2014
# Copyright:   (c) Administrator 2014
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import re
def checklen(pwd):
    return len(pwd)>=8
def checkContainUpper(pwd):
    pattern = ('[A-Z]+')
    match = (pwd)
    if match:
        return True
    else:
        return False
def checkContainNum(pwd):
    pattern = ('[0-9]+')
    match = (pwd)
    if match:
        return True
    else:
        return False
def checkContainLower(pwd):
    pattern = ('[a-z]+')
    match = (pwd)
    if match:
        return True
    else:
       return False
def checkSymbol(pwd):
    pattern = ('([^a-z0-9A-Z])+')
    match = (pwd)
    if match:
        return True
    else:
        return False
def checkPassword(pwd):
# Determine if the password length is legal
    lenOK=checklen(pwd)
# Determine if it contains uppercase letters
    upperOK=checkContainUpper(pwd)
# Determine if it contains lowercase letters
    lowerOK=checkContainLower(pwd)
# Determine if it contains numbers
    numOK=checkContainNum(pwd)
# Determine if it contains symbols
    symbolOK=checkSymbol(pwd)
    print(lenOK)
    print(upperOK)
    print(lowerOK)
    print(numOK)
    print(symbolOK)
    return (lenOK and upperOK and lowerOK and numOK and symbolOK)

def main():
    if checkPassword('Helloworld#123'):
print('Test passed')
    else:
print('Test failed')

if __name__ == '__main__':
    main()