tomjerry741

Password Generator class to your project (visual basic 2008/2010)

Mar 20th, 2012
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.90 KB | None | 0 0
  1. 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
  2.  
  3. Create a new project add a class call it cPwsGen.vb
  4. Add the code below to the class.
  5. ------------------------------------------------------------------
  6.  
  7.       Imports System.Text
  8.  
  9. Public Class cPwsGen
  10.  
  11.     'Private variables.
  12.  
  13.     Private pLen As Integer = 0
  14.  
  15.     Private pPasswordType As TGenPass
  16.  
  17.     'Private password sources
  18.  
  19.     Private Const Alpha As String = "abcdefghijklmnopqrstuvwxyz"
  20.  
  21.     Private Const Digits = "0123456789"
  22.  
  23.     Private Const Hexdecimal = "abcdef"
  24.  
  25.     Private Const SpecialChars = "!@#$%^&*()<>=/\+-*"
  26.  
  27.     Public Enum TGenPass
  28.  
  29.               Alpha = 0
  30.  
  31.               AlphaDigit = 1
  32.  
  33.               Digits = 2
  34.  
  35.               Hexdecimal = 3
  36.  
  37.               SpecialChar = 4
  38.  
  39.               SpecialAlpha = 5
  40.  
  41.               SpecialDigit = 6
  42.  
  43.           End Enum
  44.  
  45.     Private Function GetPassword(ByVal Length As Integer, Optional ByVal GenType As TGenPass = TGenPass.Alpha) As String
  46.         Dim x As Integer
  47.         Dim r As Integer
  48.         Dim CharStream As String
  49.         Dim sb As StringBuilder
  50.         sb = New StringBuilder
  51.         CharStream = vbNullString
  52.         'Password types
  53.         Select Case GenType
  54.             Case (TGenPass.Alpha)
  55.                 CharStream = Alpha
  56.             Case (TGenPass.Digits)
  57.                 CharStream = Digits
  58.             Case (TGenPass.Hexdecimal)
  59.  
  60.                 CharStream = Hexdecimal & Digits
  61.             Case (TGenPass.AlphaDigit)
  62.                 CharStream = Digits & Alpha
  63.             Case (TGenPass.SpecialChar)
  64.                 CharStream = SpecialChars
  65.  
  66.             Case (TGenPass.SpecialAlpha)
  67.  
  68.                 CharStream = Alpha & SpecialChars
  69.  
  70.             Case (TGenPass.SpecialDigit)
  71.  
  72.                 CharStream = Digits & SpecialChars
  73.  
  74.         End Select
  75.         For x = 1 To Length
  76.  
  77.         Next
  78.         'Ini Random numbers
  79.         Call Randomize()
  80.         'Get random number
  81.         r = Int(Rnd() * Len(CharStream) + 1)
  82.         'Build generated password.
  83.  
  84.         Call sb.Append(Mid$(CharStream, r, 1))
  85.  
  86.         GetPassword = sb.ToString
  87.  
  88.     End Function
  89.  
  90.     Public Property PasswordType As TGenPass
  91.  
  92.               Get
  93.  
  94.     'Return password type.
  95.  
  96.                   PasswordType = pPasswordType
  97.  
  98.               End Get
  99.  
  100.               Set(ByVal value As TGenPass)
  101.  
  102.     'Set password type.
  103.  
  104.                   pPasswordType = value
  105.  
  106.               End Set
  107.  
  108.           End Property
  109.  
  110.  
  111.     Public Property PasswordLength As Integer
  112.  
  113.               Get
  114.  
  115.     'Return password length.
  116.  
  117.                   PasswordLength = pLen
  118.  
  119.               End Get
  120.  
  121.               Set(ByVal value As Integer)
  122.  
  123.     'Set password length.
  124.  
  125.                   pLen = value
  126.  
  127.               End Set
  128.  
  129.           End Property
  130.  
  131.  
  132.     Public ReadOnly Property Password As String
  133.  
  134.  
  135.     'Return generated password.
  136.         Get
  137.             Password = GetPassword(PasswordLength, PasswordType)
  138.         End Get
  139.  
  140.  
  141.     End Property
  142.  
  143. End Class
  144. ---------------------------------------------------------------------------------
  145.  
  146.  
  147.  
  148. then
  149. in yout form , put a button
  150. For the example add this to a command button
  151. -----------------------------------------------------------------------------------
  152. Public Class Form1
  153.  
  154.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  155.         Dim GenPws As New cPwsGen
  156.         'Set password generator props
  157.         GenPws.PasswordLength = 8
  158.         GenPws.PasswordType = cPwsGen.TGenPass.AlphaDigit
  159.         'Example
  160.         Call MsgBox(GenPws.Password, vbInformation Or vbOKOnly, "Your Passsword")
  161.     End Sub
  162. End Class
  163. -------------------------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment