Check password strength: Python

Implementation

This code will validate a password on the server side. If you want to validate the password on the client side, check our Javascript. This one should work with PHP4 and PHP5.

import re
def CheckPassword(password):
    strength = ['Blank','Very Weak','Weak','Medium','Strong','Very Strong']
    score = 1

    if len(password) < 1:
        return strength[0]
    if len(password) < 4:
        return strength[1]

    if len(password) >=8:
        score = score + 1
    if len(password) >=10:
        score = score + 1
    
    if re.search('\d+',password):
        score = score + 1
    if re.search('[a-z]',password) and re.search('[A-Z]',password):
        score = score + 1
    if re.search('.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]',password):
        score = score + 1

    return strength[score]
		

Usage

Call the method CheckPassword() passing the password to be validated, and you can then do your own validation stuff based on this result. You can also change the code to return the score instead of the text.
You need to import the re (Regular Expressions) library for this to work!

How does it work?

Our scripts will test passwords and return a score which represents its strength.
The score goes from 1 (Very Weak) to 5 (Very Strong), and 0 if the value is blank. It will increase by 1 point if:

Note that if the password length is less than 4, the score will be limited to "Very Weak".