Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hi this is just a small class I made for one of my projects to generate a random password from letters,numbers and special chars easy to add to your project. Hope you find it usfull
- Create a new project add a class call it cPwsGen.vb
- Add the code below to the class.
- ------------------------------------------------------------------
- Imports System.Text
- Public Class cPwsGen
- 'Private variables.
- Private pLen As Integer = 0
- Private pPasswordType As TGenPass
- 'Private password sources
- Private Const Alpha As String = "abcdefghijklmnopqrstuvwxyz"
- Private Const Digits = "0123456789"
- Private Const Hexdecimal = "abcdef"
- Private Const SpecialChars = "!@#$%^&*()<>=/\+-*"
- Public Enum TGenPass
- Alpha = 0
- AlphaDigit = 1
- Digits = 2
- Hexdecimal = 3
- SpecialChar = 4
- SpecialAlpha = 5
- SpecialDigit = 6
- End Enum
- Private Function GetPassword(ByVal Length As Integer, Optional ByVal GenType As TGenPass = TGenPass.Alpha) As String
- Dim x As Integer
- Dim r As Integer
- Dim CharStream As String
- Dim sb As StringBuilder
- sb = New StringBuilder
- CharStream = vbNullString
- 'Password types
- Select Case GenType
- Case (TGenPass.Alpha)
- CharStream = Alpha
- Case (TGenPass.Digits)
- CharStream = Digits
- Case (TGenPass.Hexdecimal)
- CharStream = Hexdecimal & Digits
- Case (TGenPass.AlphaDigit)
- CharStream = Digits & Alpha
- Case (TGenPass.SpecialChar)
- CharStream = SpecialChars
- Case (TGenPass.SpecialAlpha)
- CharStream = Alpha & SpecialChars
- Case (TGenPass.SpecialDigit)
- CharStream = Digits & SpecialChars
- End Select
- For x = 1 To Length
- Next
- 'Ini Random numbers
- Call Randomize()
- 'Get random number
- r = Int(Rnd() * Len(CharStream) + 1)
- 'Build generated password.
- Call sb.Append(Mid$(CharStream, r, 1))
- GetPassword = sb.ToString
- End Function
- Public Property PasswordType As TGenPass
- Get
- 'Return password type.
- PasswordType = pPasswordType
- End Get
- Set(ByVal value As TGenPass)
- 'Set password type.
- pPasswordType = value
- End Set
- End Property
- Public Property PasswordLength As Integer
- Get
- 'Return password length.
- PasswordLength = pLen
- End Get
- Set(ByVal value As Integer)
- 'Set password length.
- pLen = value
- End Set
- End Property
- Public ReadOnly Property Password As String
- 'Return generated password.
- Get
- Password = GetPassword(PasswordLength, PasswordType)
- End Get
- End Property
- End Class
- ---------------------------------------------------------------------------------
- then
- in yout form , put a button
- For the example add this to a command button
- -----------------------------------------------------------------------------------
- Public Class Form1
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim GenPws As New cPwsGen
- 'Set password generator props
- GenPws.PasswordLength = 8
- GenPws.PasswordType = cPwsGen.TGenPass.AlphaDigit
- 'Example
- Call MsgBox(GenPws.Password, vbInformation Or vbOKOnly, "Your Passsword")
- End Sub
- End Class
- -------------------------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment