Solved

md5 random password generator

Posted on 2003-10-21
2
287 Views
Last Modified: 2010-04-17
I am looking for a random password generator coded in VB or C#.  It needs to save the password in md5.  Can anybody show me where to find one, or give me some resources to write one?  Thanks.
0
Comment
Question by:sbock
2 Comments
 
LVL 9

Accepted Solution

by:
malharone earned 100 total points
Comment Utility
i'm assuming the passwords you generate would not be VERY LARGE in length and/or not involve funky characters (e.g. unicode).
i once wrote a code for testdata generator, which also generates random, but "smart", strings. it gives you multitude of options for generating the output.
checkout: http://testgen.tigris.org/
you can also access the src code by going to http://testgen.tigris.org/source/browse/testgen/src/
But you'll have to be a registered user in order to access the material.
here's the deployment for the entire project (binaries -- no need for login): http://testgen.tigris.org/servlets/ProjectDocumentList?folderID=1090
screen shots: http://testgen.tigris.org/screenshots.htm

you can also download the entire sol'n (vb.net) as it is freely availabel w/ sourcecode under BSD licesne.

you'll need the following classes


Public Class RandString
    Inherits RandChar

#Region " PRIVATE PROPERTIES "
    Private mySize As Short
    Private str() As Char
    Private i As Integer
    Private uBound As Integer
#End Region

#Region " CONSTRUCTOR "
    Sub New(Optional ByVal opt As TextRandomizerOptions = RandChar.TextRandomizerOptions.AlphaMixedOnly, Optional ByVal size As Integer = -1, Optional ByVal minASCII As Integer = 33, Optional ByVal maxASCII As Integer = 126)
        MyBase.New(opt, minASCII, maxASCII)
        Me.RandomizationMode = PublicMod.DefaultRandMode
        'mySize = IIf(size < 1, Rnd(5) + 2, size)
        'mySize = mySize - 1
        'ReDim str(mySize) '= New Char(stringSize)
        Me.StringSize = size
    End Sub
#End Region

#Region " PUBLIC PROPERTY "
    Public Property StringSize() As Short
        Get
            Return Me.mySize
        End Get
        Set(ByVal Value As Short)
            mySize = IIf(Value < 1, Rnd(5) + 2, Value)
            'mySize = mySize
            uBound = mySize - 1
            ReDim str(uBound) '= New Char(stringSize)
        End Set
    End Property
#End Region

#Region " PUBLIC FUNCTIONS "
    Public Overrides Function ShallowCopy() As IRandomizer
        Return Me.MemberwiseClone()
    End Function

    Public Shadows Function DeepCopy() As IRandomizer
        Dim randStr As RandString = MyBase.DeepCopy()
        randStr.mySize = mySize
        randStr.str = Me.str
        randStr.i = Me.i
        Return randStr
    End Function

    Public Overrides Function getPermCount() As Double
        ''randomization options are irrelevant for strings (no specific boundries)
        Return 1
    End Function

    Public Overrides Function getSmartCount() As Double
        ''randomization options are irrelevant for strings (no specific boundries)
        Return 1
    End Function
    Public Overrides Function getSpecifiedCount() As Double
        Select Case Me.myRandomMode
            Case IRandomizer.RandomMode.AllPermutations
                Return Me.getPermCount()
            Case IRandomizer.RandomMode.RandomPermutations
                Return 1
            Case IRandomizer.RandomMode.Smart
                Return Me.getSmartCount()
            Case IRandomizer.RandomMode.SmartAndRandom
                Return Me.getSmartCount() + 1
        End Select
    End Function
    Public Overrides Function getNextValue() As Object
        ''randomization options are irrelevant for strings (no specific boundries)
        Select Case myTextRandomizerOption
            Case RandChar.TextRandomizerOptions.AlphaLower
                Me.retVal = randStringLowerAlphasOnly()
            Case RandChar.TextRandomizerOptions.AlphaMixedOnly
                Me.retVal = randStringAlphasOnly()
            Case RandChar.TextRandomizerOptions.AlphaNumeric
                Me.retVal = randStringAlphasDigitsOnly()
            Case RandChar.TextRandomizerOptions.AlphaUpper
                Me.retVal = randStringUpperAlphasOnly()
            Case RandChar.TextRandomizerOptions.ASCIIChar
                Me.retVal = randStringString()
            Case RandChar.TextRandomizerOptions.SpecifiedRange
                Me.retVal = randStringWithinASCIIRangeOf(myMin, myMax)
            Case Else
                Me.retVal = randStringString()
        End Select

        Return Me.myPrefix + Me.retVal + Me.mySuffix
    End Function
#End Region

#Region " PRIVATE FUNCTIONS "
    'GENERATE RANDOM STRING OF ALL KIND AND SIZE
    'generate a string that contains any combination of lower letters only
    Private Function randStringLowerAlphasOnly() As String
        Return randStringWithinASCIIRangeOf(97, 122)
    End Function

    'generate a string that contains any combination of upper letter only
    Private Function randStringUpperAlphasOnly() As String
        Return randStringWithinASCIIRangeOf(65, 90)
    End Function

    'generate a string that contains any combination of digits only
    Private Function randStringDigitsOnly() As String
        Return randStringWithinASCIIRangeOf(48, 57)
    End Function

    'generate a string that contains any combination of upper and lower letter only
    Private Function randStringAlphasOnly() As String
        For i = 0 To uBound '- 1
            str(i) = randCharAnyLetter()
        Next
        Return New String(str)
    End Function

    'generate a string that contains any combination of upper and lower letter only
    Private Function randStringAlphasDigitsOnly() As String
        'Dim str(stringSize) As Char ' char[] str = new char[stringSize];
        Dim chance As Integer
        'Dim i As Integer
        For i = 0 To uBound '- 1
            chance = (CInt((Rnd(1) * 100) Mod 2))
            str(i) = IIf((chance = 0), randCharAnyLetter(), randCharDigit())
        Next
        Return New String(str)
    End Function

    'generate a string that contains any combination of characters
    Private Function randStringString() As String
        Return randStringWithinASCIIRangeOf(33, 126)
    End Function

    'generate a string that contains any combination of characters within the given range
    Private Function randStringWithinASCIIRangeOf(ByVal minASCII As Integer, ByVal maxASCII As Integer) As String
        'Dim str(stringSize) As Char
        'Dim i As Integer
        For i = 0 To uBound  '- 1
            str(i) = randCharAnyCharWithin(minASCII, maxASCII)
        Next
        Return New String(str)
    End Function
#End Region

End Class

---------------------------------

''Checked by Malhar as of May 10th 8:54 PM
''Major changes (ALREADY DONE): re-implement class using inheritence

Public Class RandChar
    Inherits SingleItemCollection

#Region " PRIVATE PROPERTIES "
    Friend myTextRandomizerOption As TextRandomizerOptions
    Friend myMin, myMax As Integer
    Private chance As Integer
    Private size As Integer
    Private d As Double
#End Region

#Region " MISC DECLARATIONS "
    Public Enum TextRandomizerOptions
        AlphaLower = 1
        AlphaUpper = 2
        AlphaMixedOnly = 3
        AlphaNumeric = 4
        ASCIIChar = 5
        SpecifiedRange = 6
    End Enum
#End Region

#Region " PUBLIC PROPERTY "
    Public ReadOnly Property TextRandomizationOption() As TextRandomizerOptions
        Get
            Return Me.myTextRandomizerOption
        End Get
    End Property
#End Region

#Region " PRIVATE FUNCTIONS "
    Private Sub addItems(ByVal min As Short, ByVal max As Short)
        Dim x As Short
        For x = min To max
            MyBase.addItem(Convert.ToChar(x))
        Next
    End Sub
#End Region

#Region " CONSTRUCTOR "
    Sub New(Optional ByVal opt As TextRandomizerOptions = TextRandomizerOptions.AlphaMixedOnly, Optional ByVal minASCII As Integer = 33, Optional ByVal maxASCII As Integer = 126)
        MyBase.New()

        Me.RandomizationMode = PublicMod.DefaultRandMode
        Me.clearList()
        myMin = IIf(minASCII < 33, 33, minASCII)
        myMax = IIf(maxASCII > 126, 126, maxASCII)
        myTextRandomizerOption = opt

        Select Case Me.myTextRandomizerOption
            Case TextRandomizerOptions.AlphaLower
                addItems(97, 122)
            Case TextRandomizerOptions.AlphaMixedOnly
                addItems(65, 90)
                addItems(97, 122)
            Case TextRandomizerOptions.AlphaNumeric
                addItems(48, 57)
                addItems(65, 90)
                addItems(97, 122)
            Case TextRandomizerOptions.AlphaUpper
                addItems(65, 90)
            Case TextRandomizerOptions.ASCIIChar
                addItems(33, 126)
            Case TextRandomizerOptions.SpecifiedRange
                addItems(myMin, myMax)
        End Select
    End Sub
#End Region

#Region " FRIEND FUNCTIONS "
    Friend Function randCharDigit() As Char
        'GENERATE RANDOM SINGLE CHARACTER.
        'generate a random digit of type char from 0 - 9.ASCII: 48 - 57
        Return randCharAnyCharWithin(48, 57)
    End Function

    Friend Function randCharUpperLetter() As Char
        'generate an uppercase letter from A to Z.ASCII: 65 - 90
        Return randCharAnyCharWithin(65, 90)
    End Function

    Friend Function randCharLowerLetter() As Char
        'generate a lower case letter from a to z.ASCII: 97 - 122
        Return randCharAnyCharWithin(97, 122)
    End Function

    Friend Function randCharAnyLetter() As Char
        chance = CInt(Rnd(1) * 10) Mod 2
        Return IIf(chance = 0, randCharLowerLetter(), randCharUpperLetter())
        ''Return randCharAnyCharWithin(65, 122)
    End Function

    Friend Function randCharAnyCharacter() As Char
        'generate any printable character.ASCII: 33 - 126
        Return randCharAnyCharWithin(33, 126)
    End Function

    Friend Function randCharAnyCharWithin(ByVal minASCIIorder As Integer, ByVal maxASCIIorder As Integer) As Char
        'generate any character within the given range
        'the range must be valid (within 33 - 126)
        size = maxASCIIorder - minASCIIorder
        d = Rnd(1) * size

        'method round(double) is used to cover the boundary random value
        Return Convert.ToChar(CInt(Math.Round(d)) + minASCIIorder)
    End Function
#End Region

#Region " PUBLIC FUNCTIONS "
    Public Overrides Function ShallowCopy() As IRandomizer
        Dim newRandChar As RandChar = MyBase.ShallowCopy
        With newRandChar
            .myTextRandomizerOption = Me.myTextRandomizerOption
            .myMin = Me.myMin
            .myMax = Me.myMax
            .size = Me.size
            .d = d
        End With
        Return newRandChar
    End Function

    Public Overrides Function DeepCopy() As IRandomizer
        Dim newRandChar As RandChar = MyBase.DeepCopy()
        With newRandChar
            .myTextRandomizerOption = Me.myTextRandomizerOption
            .myMin = Me.myMin
            .myMax = Me.myMax
            .size = Me.size
            .d = d
        End With
        Return newRandChar
    End Function
#End Region

End Class

------------------------------------------------------------------

''Checked by Malhar as of May 4th 5:48 PM & May 10th 12:08 AM
''EXCEPTIONS THROWN: ListEmptyException (when trying to getNextValue and the collection is empty)

Option Explicit On
Public Class SingleItemCollection
    Implements IRandomizer

#Region " FRIEND PROPERTIES "
    Friend myCollection As ArrayList
    Friend myCount As Long
    Friend myPrefix As String = ""
    Friend mySuffix As String = ""
    Friend numExported As Double = 0
    Friend myRandomMode As IRandomizer.RandomMode = DefaultRandMode
    Friend retVal As String
#End Region

#Region " MISC. DECLARATIONS "
    Event DataGenerationCompleted() Implements IRandomizer.DataGenerationCompleted
#End Region

#Region " PUBLIC PROPERTIES "
    Public ReadOnly Property ItemCount() As Long
        Get
            Return myCount
        End Get
    End Property

    Public Property RandomizationMode() As IRandomizer.RandomMode Implements IRandomizer.RandomizationMode
        Get
            Return Me.myRandomMode
        End Get
        Set(ByVal Value As IRandomizer.RandomMode)
            Me.myRandomMode = Value
            numExported = 0
        End Set
    End Property

    Public Property Prefix() As String Implements IRandomizer.Prefix
        Get
            Return myPrefix
        End Get
        Set(ByVal Value As String)
            myPrefix = Value
        End Set
    End Property

    Public Property Suffix() As String Implements IRandomizer.Suffix
        Get
            Return mySuffix
        End Get
        Set(ByVal Value As String)
            mySuffix = Value
        End Set
    End Property
#End Region

#Region " CONSTRUCTOR "
    Sub New()
        Me.New(New String() {})
    End Sub

    Sub New(ByVal newItem As String)
        Me.New(New Object() {newItem})
    End Sub

    Sub New(ByVal itemARL As ArrayList)
        Me.New(itemARL.ToArray())
    End Sub

    Sub New(ByVal itemAR() As String)
        Me.RandomizationMode = PublicMod.DefaultRandMode
        clearList()
        Dim st As String
        For Each st In itemAR
            addItem(st)
        Next
    End Sub
#End Region

#Region " PUBLIC FUNCTIONS "
    Public Sub clearList()
        myCount = 0
        Me.myCollection = New ArrayList(16)
        numExported = 0
    End Sub

    Public Sub addItem(ByVal newItem As String)
        myCollection.Add(newItem)
        myCount += 1
    End Sub

    Public Overridable Function getNextValue() As Object Implements IRandomizer.getNextValue
        If myCount < 1 Then
            Throw New ListEmptyException("Single Item Collection")
        Else
            Select Case Me.myRandomMode
                Case IRandomizer.RandomMode.AllPermutations
                    retVal = myCollection(numExported)
                    If (numExported + 1) >= myCount Then
                        numExported = 0
                        RaiseEvent DataGenerationCompleted()
                    Else
                        numExported += 1
                    End If
                Case IRandomizer.RandomMode.RandomPermutations
                    retVal = myCollection.Item(Int(myCount * Rnd()))
                Case IRandomizer.RandomMode.Smart, IRandomizer.RandomMode.SmartAndRandom
                    Select Case numExported
                        Case 0
                            retVal = myCollection.Item(0)
                        Case 1
                            retVal = myCollection.Item(myCount / 2)
                        Case 2
                            retVal = myCollection.Item(myCount - 1)
                            RaiseEvent DataGenerationCompleted()
                        Case Else
                            If Me.myRandomMode = IRandomizer.RandomMode.SmartAndRandom Then
                                Me.RandomizationMode = IRandomizer.RandomMode.RandomPermutations
                            Else
                                Me.numExported = 0
                            End If
                            Return Me.getNextValue()
                    End Select
                    numExported += 1
            End Select
            Return myPrefix & retVal & mySuffix
        End If
    End Function

    Public Overridable Function getPermCount() As Double Implements IRandomizer.getPermCount
        Return myCount
    End Function

    Public Overridable Function getSpecifiedCount() As Double Implements IRandomizer.getSpecifiedCount
        Select Case Me.myRandomMode
            Case IRandomizer.RandomMode.AllPermutations
                Return Me.getPermCount()
            Case IRandomizer.RandomMode.RandomPermutations
                Return 1
            Case IRandomizer.RandomMode.Smart
                Return Me.getSmartCount()
            Case IRandomizer.RandomMode.SmartAndRandom
                Return Me.getSmartCount() + 1
        End Select
    End Function
    Public Overridable Function getSmartCount() As Double Implements IRandomizer.getSmartCount
        Return Math.Min(myCount, 3)
    End Function
    Public Overridable Function ShallowCopy() As IRandomizer Implements IRandomizer.ShallowCopy
        Return Me.MemberwiseClone
    End Function

    Public Overridable Function DeepCopy() As IRandomizer Implements IRandomizer.DeepCopy
        Dim iRand As SingleItemCollection = Me.ShallowCopy
        Dim myNewColl As New ArrayList(Me.myCollection.Count)
        Dim obj As String
        For Each obj In Me.myCollection
            myNewColl.Add(String.Copy(obj))
        Next
        iRand.myCollection = myNewColl
        Return iRand
    End Function
#End Region

End Class

---------------------------------

'' Notes from Malhar: (no additional methods are planned to be added to this interface as of May 11th 2003 3:33 AM
Public Interface IRandomizer ' implicitly extends System.Object
    'Inherits ICloneable

    Function getNextValue() As Object
    Function getSmartCount() As Double  'double with 0 decimal places
    Function getPermCount() As Double   'double with 0 decimal places
    Function getSpecifiedCount() As Double
    Function ShallowCopy() As IRandomizer
    Function DeepCopy() As IRandomizer

    Property RandomizationMode() As RandomMode
    Property Prefix() As String
    Property Suffix() As String

    Event DataGenerationCompleted()

    Enum RandomMode
        AllPermutations = 1
        Smart = 2
        SmartAndRandom = 4
        RandomPermutations = 3
    End Enum
End Interface
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now