The following is a simple random number generator with a few sub functions broken out to generate a random number and random true or false. I created a utility class which I will continue to build to meet new projects and from previous asp.net functions.
The following are what I consider the most commonly confused letters for numbers.
B = 8 (eight)
I = 1 (one)
L = 1 (one)
O = 0 (zero
S = 5 (five)
Z = 2 (two)
So basically the purpose of knowing this is to remove those letters and numbers from a username or password generator.
I googled for more information on this topic and couldn't find anything and I don't consider what I have as definitive, it's just what I could figure out.
I'm sure there is a better (more efficient) way to do most if not all of the functions below. If you have a suggestion feel free to post it in the comments.
Public Class Utility
Public Shared Function SanitizeSQL(ByVal Input As String) As String
'Strip Quotes
Input = Input.Replace("'", "''")
'Remove Harmful Chars
Input = Input.Replace("SELECT", "").Replace("drop", "").Replace(";", "").Replace("--", "").Replace("insert", "").Replace("delete", "").Replace("xp_", "").Replace("|", "")
Return Input
End Function
Public Shared Function MySQL_Date_Now() As String
Return Format(Date.Now, "yyyy-MM-dd hh:mm:ss tt")
End Function
Public Shared Function MySQL_Date_Convert(ByVal Input As String) As DateTime
Return Format(Date.Parse(Input), "yyyy-MM-dd hh:mm:ss tt")
End Function
Public Enum PasswordType As Integer
LowerLettersOnly = 1
UpperLettersOnly = 2
NumbersOnly = 3
NumbersAndLowerLetters = 4
NumbersAndUpperLetters = 5
NumbersAndMixedCaseLetters = 6
End Enum
Public Shared Function Random_Password( _
Optional ByVal Length As Integer = 8 _
, Optional ByVal Type As PasswordType = 1 _
, Optional ByVal AddChars As String = "" _
, Optional ByVal RemoveChars As String = "" _
) As String
Dim Numbers_Bag = "34679" 'change to include more numbers
Dim Letters_Bag = "acdefghjkmnpqrtuwvxy" 'change to include more letters
Dim CharBag As String = ""
If Type >= PasswordType.NumbersOnly Then
CharBag = Numbers_Bag
End If
If Type <> PasswordType.NumbersOnly Then
CharBag += Letters_Bag
End If
CharBag += AddChars
For counter As Integer = 0 To RemoveChars.Length - 1
If CharBag.IndexOf(RemoveChars.Substring(counter, 1)) >= 0 Then
CharBag = CharBag.Remove(CharBag.IndexOf(RemoveChars.Substring(counter, 1)), 1)
End If
Next counter
'Begin Creating Random Password
Dim temp_string As New System.Text.StringBuilder
For t As Byte = 1 To Length
Dim random_char As Integer
Randomize()
random_char = Rnd() * (Len(CharBag) - 1)
If Type = PasswordType.NumbersAndMixedCaseLetters And Random_True() = True Then
temp_string.Append(CharBag.ToUpper.Trim.Chars(random_char))
Else
temp_string.Append(CharBag.Trim.Chars(random_char))
End If
Next
Select Case Type
Case PasswordType.NumbersAndUpperLetters, PasswordType.UpperLettersOnly
Return temp_string.ToString.ToUpper
Case Else
Return temp_string.ToString
End Select
End Function
Public Shared Function Random_True() As Boolean
Return Random_Number(0, 1)
End Function
Public Shared Function Random_Number( _
Optional ByVal LowerLimit As Integer = 0 _
, Optional ByVal UpperLimit As Integer = 100000 _
) As Integer
Randomize()
Return CInt(Int(((UpperLimit - LowerLimit + 1) * Rnd()) + Int(LowerLimit)))
'from MSDN Help - randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
End Function
End Class
No comments:
Post a Comment