B4J Code Snippet Measure the password strength

Measure the password strength

Now check if your password is really secure?

With this function, you can determine the strength of a password. The return values give the user hints where weaknesses are and where something can be improved. There are no official formulas for calculating password security. Therefore, this function uses its own weighting system. Such calculations can never be perfect, but they provide the user with a guide to create better passwords. (translate with google)
B4X:
'0-25         F--  deficient DONT USE!
'20-45        F    poor
'40-65        E    sufficient   
'60-85        D    satisfactory
'80-120       C    good
'100-145      B    strong enough
'140-200      A    very strong
'200+         A++  best choice
Sub CheckPasswordStrenght( Password As String) As Int
    'according to an idea by:
    'http://www.passwordmeter.com/

    Dim CountChars         As Int = Password.Length

    Dim hasDigits         As Boolean = Regex.IsMatch( ".*\d.*", Password)
    Dim hasUpperCases     As Boolean = Regex.IsMatch( ".*\p{Lu}.*", Password)
    Dim hasLowerCases     As Boolean = Regex.IsMatch( ".*\p{Ll}.*", Password)
    Dim hasSymbols         As Boolean = Regex.IsMatch( ".*\p{P}.*", Password)
   
    Dim OnlyDigits         As Boolean = Regex.IsMatch( "\d+", Password)
    Dim OnlyUpperCases     As Boolean = Regex.IsMatch( "\p{Lu}+", Password)
    Dim OnlyLowerCases     As Boolean = Regex.IsMatch( "\p{Ll}+", Password)
    Dim OnlyLetters        As Boolean = Regex.IsMatch( "\p{L}+", Password)
   
    Dim Digits3Times     As Boolean = Regex.IsMatch( ".*\d{3,}.*", Password)
    Dim Upper3Times        As Boolean = Regex.IsMatch( ".*\p{Lu}{3,}.*", Password)
    Dim Lower3Times        As Boolean = Regex.IsMatch( ".*\p{Ll}{3,}.*", Password)
    Dim Letters3Times    As Boolean = Regex.IsMatch( ".*\p{L}{3,}.*", Password)
   
    Dim Strenght         As Int = 0
   
    Strenght = Strenght +( CountChars*4)
    If CountChars > 11 Then Strenght = Strenght +( CountChars*4)
   
    If hasDigits Then
        Dim nDigits As Int = CountMatches( "\d", Password)
        Strenght = Strenght +(nDigits*4)
    End If
    If hasUpperCases Then
        Dim nUpper As Int = CountMatches( "\p{Lu}", Password)
        Strenght = Strenght +(( CountChars-nUpper)*2)
    End If
    If  hasLowerCases Then
        Dim nLower As Int = CountMatches( "\p{Ll}", Password)
        Strenght = Strenght +(( CountChars-nLower)*2)
    End If
    If hasSymbols Then
        Dim nSpecial As Int = CountMatches( "\p{P}", Password)
        Strenght = Strenght +(nSpecial*6)
    End If
    If hasDigits And hasLowerCases And hasUpperCases And hasSymbols Then
        Strenght = Strenght +((nDigits+nUpper+nLower+nSpecial) *2)
    End If
   
    If OnlyDigits         Then Strenght = Strenght - nDigits
    If OnlyUpperCases     Then Strenght = Strenght - nUpper
    If OnlyLowerCases     Then Strenght = Strenght - nLower
    If OnlyLetters         Then Strenght = Strenght - (nUpper+nLower)
    If Digits3Times     Then Strenght = Strenght - (nDigits *3)
    If Upper3Times        Then Strenght = Strenght - (nUpper *3)
    If Lower3Times        Then Strenght = Strenght - (nLower *3)
    If Letters3Times     Then Strenght = Strenght - ((nLower+nUpper) *3)

    Return Strenght
End Sub

Sub CountMatches( Match As String, toMatch As String) As Int
    Dim nCount As Int = 0
    Dim mt As Matcher
    mt = Regex.Matcher( Match, toMatch)
    Do While mt.Find
        nCount=nCount+1
    Loop
    Return nCount
End Sub

This function I have written to learn and understand regex.
 
Top