Android Code Snippet Password generator Sub

Hi. I needed to generate passwords in my app, so I wrote the code below, but calling the sub with strings, like "(6, "NUMBERS")", or "(8, "NUMBERS_UPPERCASE_SYMBOLS")". When I was going to share the code here as a snippet I saw this thread, and then I took the idea of calling the sub with arguments (True, True, False, False), instead of using a string.

So, although the library exists, perhaps someone prefers or finds easier to use a Sub, or wants to customize the characters to use (to avoid similar characters, like "I1", "O0", for example). Here it is:

B4X:
Sub GenerateRandomPassword(numChars As Int, numbers As Boolean, lowercase As Boolean, uppercase As Boolean, symbols As Boolean) As String

   Dim newPassword As String
          
   Dim uppercaseArray(26) As String = Array As String ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
   Dim lowercaseArray(26) As String = Array As String ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
   Dim numbersArray(10)   As String = Array As String ("0","1","2","3","4","5","6","7","8","9")
   Dim symbolsArray(12)   As String = Array As String ("@","$","%","&","?","#","!","*","+","-",";","_")
  
   Dim charList As List
       charList.Initialize
  
   If numbers   Then charList.AddAll(numbersArray)
   If lowercase Then charList.AddAll(lowercaseArray)
   If uppercase Then charList.AddAll(uppercaseArray)
   If symbols   Then charList.AddAll(symbolsArray)
  
   For i = 1 To numChars
     newPassword = newPassword & charList.Get(Rnd(0, charList.Size))
   Next  
 
   Return newPassword
  
End Sub

Usage is very simple too:
B4X:
Dim pwd As String =   GenerateRandomPassword(6, False, True, True, False)

LogColor ("New password: " & pwd, Colors.magenta)
 

Ivan Aldaz

Member
Licensed User
Longtime User
This way, isn't it?

B4X:
Sub GenerateRandomPassword(numChars As Int, numbers As Boolean, lowercase As Boolean, uppercase As Boolean, symbols As Boolean) As String
          
   Dim uppercaseArray(26) As String = Array As String ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
   Dim lowercaseArray(26) As String = Array As String ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
   Dim numbersArray(10)   As String = Array As String ("0","1","2","3","4","5","6","7","8","9")
   Dim symbolsArray(12)   As String = Array As String ("@","$","%","&","?","#","!","*","+","-",";","_")
  
   Dim charList As List
       charList.Initialize
  
   If numbers   Then charList.AddAll(numbersArray)
   If lowercase Then charList.AddAll(lowercaseArray)
   If uppercase Then charList.AddAll(uppercaseArray)
   If symbols   Then charList.AddAll(symbolsArray)

   Dim newPassword As StringBuilder
       newPassword.Initialize
  
   For i = 1 To numChars
       newPassword.Append(charList.Get(Rnd(0, charList.Size)))
   Next  
  
   LogColor ("New password: " & newPassword, Colors.magenta)  
   Return newPassword
  
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Two small changes:
1. No need to set the array length as you assign a new array anyway.
2. It is better to explicitly call ToString to convert StringBuilder to a string.

B4X:
Sub GenerateRandomPassword(numChars As Int, numbers As Boolean, lowercase As Boolean, uppercase As Boolean, symbols As Boolean) As String
          
   Dim uppercaseArray() As String = Array As String ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
   Dim lowercaseArray() As String = Array As String ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
   Dim numbersArray()   As String = Array As String ("0","1","2","3","4","5","6","7","8","9")
   Dim symbolsArray() As String = Array As String ("@","$","%","&","?","#","!","*","+","-",";","_")
  
   Dim charList As List
       charList.Initialize
  
   If numbers   Then charList.AddAll(numbersArray)
   If lowercase Then charList.AddAll(lowercaseArray)
   If uppercase Then charList.AddAll(uppercaseArray)
   If symbols   Then charList.AddAll(symbolsArray)

   Dim newPassword As StringBuilder
       newPassword.Initialize
  
   For i = 1 To numChars
       newPassword.Append(charList.Get(Rnd(0, charList.Size)))
   Next  
  
   LogColor ("New password: " & newPassword.ToString, Colors.magenta)  
   Return newPassword
  
End Sub
 
Top