Random Password Generator

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

2 Likes

my approch some times ago :

Public Function RandomString(stringLength as Integer, randomType as randomStringEnum = randomStringEnum.mixed) as String
  
  Dim res As String
  Dim myRandom As New Random
  
  For i As Integer = 1 To stringLength
    
    Select Case randomType
      
    Case randomStringEnum.mixed
      res = res + Chr( myRandom.InRange(48,122))
    Case randomStringEnum.onlyLowercase
      res = res + Lowercase( Chr( myRandom.InRange(65,90)))
    Case randomStringEnum.onlyUppercase
      res = res + Chr( myRandom.InRange(48,90))
    Case randomStringEnum.onlyLowercaseNoNumber
      res = res + Chr( myRandom.InRange(97,122))
    Case randomStringEnum.onlyUppercaseNoNumber
      res = res + Chr( myRandom.InRange(65,90))
    Case randomStringEnum.onlyNumbers
      res = res + Chr( myRandom.InRange(48,57))
    Case randomStringEnum.onlyNumbersNozero
      res = res + Chr( myRandom.InRange(49,57))
      
    End Select
  Next
  
  Return res
  
End Function

with

Public Enum randomStringEnum
onlyNumbers
onlyUppercaseNoNumber
onlyLowercaseNoNumber
mixed
onlyUppercase
onlyLowercase
onlyNumbersNozero
End Enum

and yours does not compile !
error on
Const all = Uppercase + Lowercase + numbers + symbols
and every reference to all after that

ah yeah I left off the constant definitions
will fix

while similar yours & mine are different in certain respects
mine lets me specify a “rule” about how many numbers, letters, symbols etc are in each which can have a large influence on the generated passwords strength (something else I have a method for that I’ll post)

I also made one using a big list (in a sqlite db) with english and french words, that you modify a certain amount of letters to generate the password. adding a certain qty of upper lower number or special characters.

well … if you’d like to share your code that WAS kind of the point of my post
not oneupmanship

In the spirit of sharing useful things, I once needed to translate Paul’s window-based password generator into a class-based one to untangle the logic from the user interface elements.

I’ve uploaded it here, and you’re welcome to use this in any project you need: