Skip to content

Instantly share code, notes, and snippets.

@devtimi
Created April 8, 2021 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devtimi/dca89efd118dd8c46d25e6cea78cf252 to your computer and use it in GitHub Desktop.
Save devtimi/dca89efd118dd8c46d25e6cea78cf252 to your computer and use it in GitHub Desktop.
Class based secure password generation with options
#tag Class
Protected Class PasswordGenerator
#tag Method, Flags = &h0
Sub Constructor()
if PasswordGenerator.marsDigits.Ubound < 0 then
// Add digits, skipping 0 since its hard to distinguish from O
for i as Integer = 1 to 9
marsDigits.Append(str(i))
next
end
if PasswordGenerator.marsLetters.Ubound < 0 then
// Add uppercase alphabet characters
for i as Integer = 65 to 90
if i = 79 then continue // Skip O
marsLetters.Append(Encodings.UTF8.Chr(i))
next
// Add lowercase alphabet characters
for i as Integer = 97 to 122
if i = 111 then continue // Skip o
marsLetters.Append(Encodings.UTF8.Chr(i))
next
end
if PasswordGenerator.marsSymbols.Ubound < 0 then
// Add first set of symbols
for i as Integer = 33 to 47
if i = 34 or i = 39 then continue // Skip quotes
marsSymbols.Append(Encodings.UTF8.Chr(i))
next
// More symbols
for i as Integer = 58 to 64
marsSymbols.Append(Encodings.UTF8.Chr(i))
next
// More symbols
for i as Integer = 91 to 95
marsSymbols.Append(Encodings.UTF8.Chr(i))
next
end
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Function GeneratePassword() As String
var arsPassword() as String
// Add digits first, no more than password length
for i as Integer = 1 to Min(me.DigitCount, me.Length)
arsPassword.Append(RandomDigit)
next
var tiPasswordSize as Integer = arsPassword.Ubound + 1
// Add symbols, but do not exceed password length
for i as Integer = 1 to Min(me.SymbolCount, me.Length - tiPasswordSize)
arsPassword.Append(RandomSymbol)
next
// Finally add remaining alphabet characters to reach password length
tiPasswordSize = arsPassword.Ubound + 1
for i as Integer = 1 to (me.Length - tiPasswordSize)
arsPassword.Append(RandomLetter)
next
arsPassword.Shuffle
return Join(arsPassword, "")
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function RandomDigit() As String
PasswordGenerator.marsDigits.Shuffle
return PasswordGenerator.marsDigits(0)
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function RandomLetter() As String
PasswordGenerator.marsLetters.Shuffle
return PasswordGenerator.marsLetters(0)
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function RandomSymbol() As String
PasswordGenerator.marsSymbols.Shuffle
return PasswordGenerator.marsSymbols(0)
End Function
#tag EndMethod
#tag Note, Name = About
// This class is derived from the Password Generator example in the Xojo blog.
// https://blog.xojo.com/2018/06/29/just-code-challenge-week2/
#tag EndNote
#tag Property, Flags = &h0
DigitCount As Integer = 2
#tag EndProperty
#tag Property, Flags = &h0
Length As Integer = 14
#tag EndProperty
#tag Property, Flags = &h21
Private Shared marsDigits() As String
#tag EndProperty
#tag Property, Flags = &h21
Private Shared marsLetters() As String
#tag EndProperty
#tag Property, Flags = &h21
Private Shared marsSymbols() As String
#tag EndProperty
#tag Property, Flags = &h0
SymbolCount As Integer = 2
#tag EndProperty
#tag ViewBehavior
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="DigitCount"
Visible=false
Group="Behavior"
InitialValue="2"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Length"
Visible=false
Group="Behavior"
InitialValue="14"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="SymbolCount"
Visible=false
Group="Behavior"
InitialValue="2"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment