Something I chunked out the other day
Protected Function RandomPassword(length as integer = 16, uppercaseCount as integer = -1, lowercaseCount as integer = -1, numbersCount as integer = -1, symbolsCount as integer = -1) as string
Const Lowercase as String = "abcdefghijklmnopqrstuvwxyz"
Const numbers as String = "0123456789"
Const symbols as String = "!""#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
Const Uppercase as String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// total of all the counts cant be more than the length
If uppercaseCount + lowercaseCount + numbersCount + symbolsCount > length Then
Dim tmp As New UnsupportedOperationException
tmp.message = "uppercase + lowercase + numbers + symbols > length"
Raise tmp
End If
Const all = Uppercase + Lowercase + numbers + symbols
Dim password As String = ""
Dim allcountsSatisfied As Boolean
Dim r As New Random
While allcountsSatisfied = False
password = ""
For index As Integer = 1 To length
Dim randomNumber As Integer = r.InRange(1, all.Len)
password = password + Mid(all, randomNumber, 1)
Next
allcountsSatisfied = true
Dim upperCaseCounter As Integer
Dim lowercaseCounter As Integer
Dim numbersCounter As Integer
Dim symbolsCounter As Integer
// we count chars using ASC and MID because Xojo IS NOT case sensitive but we need to be !
For i As Integer = 1 To Len(password)
Dim chASc As Integer = Asc(password.Mid(i,1))
Dim j As Integer
For j = 1 To Len(Uppercase)
If chASc = Asc(Mid(Uppercase,j,1)) Then
upperCaseCounter = upperCaseCounter + 1
End If
Next
For j = 1 To Len(Lowercase)
If chASc = Asc(Mid(Lowercase,j,1)) Then
lowercaseCounter = lowercaseCounter + 1
End If
Next
For j = 1 To Len(numbers)
If chASc = Asc(Mid(numbers,j,1)) Then
numbersCounter = numbersCounter + 1
End If
Next
For j = 1 To Len(symbols)
If chASc = Asc(Mid(symbols,j,1)) Then
symbolsCounter = symbolsCounter + 1
End If
Next
Next
If uppercaseCount > 0 Then
allcountsSatisfied = allcountsSatisfied And ( upperCaseCounter >= uppercaseCount )
End If
If lowercaseCount > 0 Then
allcountsSatisfied = allcountsSatisfied And ( lowercaseCounter >= lowercaseCount )
End If
If numbersCount > 0 Then
allcountsSatisfied = allcountsSatisfied And ( numbersCounter >= numbersCount )
End If
If symbolsCount > 0 Then
allcountsSatisfied = allcountsSatisfied And ( symbolsCounter >= symbolsCount )
End If
Wend
Return password
End Function
comments critiques & complaints welcomed