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:
Usage is very simple too:
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)