A catalogue of my discoveries in software development and related subjects, that I think might be of use or interest to everyone else, or to me when I forget what I did!
Simple Password Generator
Thursday, 28 August 2008
To generate random passwords I have written a simple class in VB.NET, which accepts a string containing valid password characters and then generates passwords of n length using the specified characters.
Public Class PasswordGenerator
Private _pwChars As String
Public Sub New()
MyClass.New("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
End Sub
Public Sub New(ByVal validChars As String)
_pwChars = validChars
End Sub
Public Function GeneratePassword(ByVal length As Integer) As String
Dim password As String = ""
Dim rndNum As New Random()
For i As Integer = 1 To length
password &= _pwChars.Substring(rndNum.Next(0, _pwChars.Length - 1), 1)
Next
Return password
End Function
End Class