'---------------------------------------------------------------------------------------
' Module : PasswordAnalysis
' Author : Galaxiom
' Date : 7/06/2011
' Updated : 10/12/2014 to correct argument declaration to ByVal
' Purpose : Testing complexity of string such as passwords
'---------------------------------------------------------------------------------------
Option Compare Database
Option Explicit
Enum pwRequired
pwNum = 1 ' Require at least one numeric character
pwAlpha = 2 ' Require at least one Alpha character
pwMixCase = 4 ' Require at least one of each both upper and low case characters
pwSpecial = 8 ' Require at least one special character
End Enum
'---------------------------------------------------------------------------------------
' Procedure : RequiredCharacters
' Author : Galaxiom
' Date : 7/06/2011
' Purpose : Test a string for:
' Minimum number of characters
' Presence of at least one of each of the designated character groups
' Usage : Enter the minimum number of characters as the MinLength Argument
' Add together each type of pwRequired type to the ReqChars argument
' (pwAlpha is not required with pwMixCase)
' Optionally set UseDialog to True to pop up a message about missing requirements
' Example : RequiredCharacters(YourString, 4, pwNum + pwMixCase + pwSpecial, True)
'---------------------------------------------------------------------------------------
'
Public Function RequiredCharacters(ByVal TestString As String, ByVal MinLength As Integer, ByVal ReqChars As pwRequired, _
Optional ByVal UseDialog As Boolean = False) As Integer
' RETURN CODES:
' Characters Accepted = 0
' SUMMED RETURN CODES:
' Numeral required = 1
' Alpha or Lowercase character required = 2
' UpperCase character required = 4
' Special character required = 8
' Insufficient Characters = 16
Dim StringLen As Integer
Dim Char As Integer '.... ASCII value of character
Dim i As Integer
' Load pwAlpha if pwMixCase (effect: require lowercase)
' Otherwise render TestString to lowercase (effect: detect uppercase as lower)
If ReqChars <> (ReqChars And Not pwMixCase) Then
ReqChars = ReqChars Or pwAlpha
Else
TestString = LCase(TestString)
End If
' Include all active pwRequired values and length code in function return
RequiredCharacters = ReqChars Or 16
StringLen = Len(TestString)
' Remove each found RequiredCode from function return
If Not StringLen < MinLength Then: RequiredCharacters = RequiredCharacters And Not 16
For i = 1 To StringLen
Char = Asc(Mid(TestString, i, 1))
If (Char > 46 And Char < 58) Then ' Numeric
RequiredCharacters = RequiredCharacters And Not pwNum
ElseIf (Char > 96 And Char < 123) Then ' LowerCase
RequiredCharacters = RequiredCharacters And Not pwAlpha
ElseIf (Char > 64 And Char < 91) Then ' UpperCase
RequiredCharacters = RequiredCharacters And Not pwMixCase
Else ' Special
RequiredCharacters = RequiredCharacters And Not pwSpecial
End If
Next
If UseDialog And RequiredCharacters Then: RequiredCharsDialog (RequiredCharacters)
End Function
=======================================
Public Function RequiredCharsDialog(RequiredCode As Integer)
' Note: When using pwAlpha the "missing requirement" will be "At least one lowercase character"
' even though an uppercase character would be accepted.
Dim NotPresent As String
If RequiredCode <> 0 Then
NotPresent = "The string still requires:" & vbCrLf & vbCrLf
If (RequiredCode And Not 15) = 16 Then: NotPresent = NotPresent & "Additional characters" & vbCrLf
If (RequiredCode And Not 23) = 8 Then: NotPresent = NotPresent & "At least one special character" & vbCrLf
If (RequiredCode And Not 27) = 4 Then: NotPresent = NotPresent & "At least one uppercase Character" & vbCrLf
If (RequiredCode And Not 29) = 2 Then: NotPresent = NotPresent & "At least one lowercase Character" & vbCrLf
If (RequiredCode And Not 30) = 1 Then: NotPresent = NotPresent & "At least one numeric character" & vbCrLf
MsgBox NotPresent
End If
End Function
=================================
Private Sub TestRequiredCharacters()
If RequiredCharacters("1aA!", 4, pwNum + pwMixCase + pwSpecial, True) = False Then
MsgBox "Accepted"
End If
End Sub
' ============= Notes =================
' The functions make extensive use of the Bitwise Operators.
' Each pwRequired value uses a single bit to indicate its inclusion in ReqChar.
' (Hence the binary pattern in their decimal values)
' The bitwise operators allow the bits to be operated on independently.
' This makes them useful where multiple independent values are held as a single number.
' X Or Y adds the bits without "carrying" as in arithmetic addition.
' X And Not Y subtracts the bits in the second operand from the first.
' Effectively this is subtraction without "borrowing".
' ==========================================