Generate random string using UDF

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Generate random string using UDF

Post by YasserKhalil »

Hello everyone
I have the following udf that generates random string specifying the needed number of capital letters and small letters and so on.

Code: Select all

Sub Test_GenerateRandomString_UDF()
    Debug.Print GenerateRandomString(5, 1, 1, 2, False)
    Debug.Print GenerateRandomString(3, 2, 3, 1, True, Range("A4").Value)
End Sub

Function GenerateRandomString(ByVal includeCapitalLetters As Long, ByVal includeSmallLetters As Long, ByVal includeNumbers As Long, ByVal includeSymbols As Long, Optional ByVal includeDate As Boolean = False, Optional ByVal myDate As String = vbNullString) As String
    Dim s As String, p As String, t As String, symbols As String, n As Long, i As Long, j As Long
    symbols = "!@#$%^&*()_-+={}[]|\:;""'<>,.?/"
    For i = 1 To includeCapitalLetters
        n = Int(26 * Rnd()) + 65
        s = s & Chr(n)
    Next i
    For i = 1 To includeSmallLetters
        n = Int(26 * Rnd()) + 97
        s = s & Chr(n)
    Next i
    For i = 1 To includeNumbers
        n = Int(10 * Rnd()) + 48
        s = s & Chr(n)
    Next i
    For i = 1 To includeSymbols
        n = Int(Len(symbols) * Rnd()) + 1
        s = s & Mid(symbols, n, 1)
    Next i
    For i = Len(s) To 2 Step -1
        j = Int(i * Rnd() + 1)
        p = Mid(s, i, 1)
        Mid(s, i, 1) = Mid(s, j, 1)
        Mid(s, j, 1) = p
    Next i
        If InStr(1, symbols, Left(s, 1)) > 0 Then
        Do
            j = Int(Len(s) - 1)
        Loop Until Not Mid(s, j + 1, 1) Like symbols
        p = Left(s, 1)
        Mid(s, 1, 1) = Mid(s, j + 1, 1)
        Mid(s, j + 1, 1) = p
    End If
    If includeDate Then
        t = IIf(myDate = vbNullString, "-" & Format(Date, "ddmmyyyy"), "-" & Format(myDate, "ddmmyyyy"))
    End If
    GenerateRandomString = s & IIf(includeDate, t, Empty)
End Function
I just need one modification which is to skip any symbol in the output to be the first character. I mean the random output would start with numbers or capital letters or small letters but not a symbol.

User avatar
HansV
Administrator
Posts: 78407
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Generate random string using UDF

Post by HansV »

How about

Code: Select all

Function GenerateRandomString( _
        ByVal includeCapitalLetters As Long, _
        ByVal includeSmallLetters As Long, _
        ByVal includeNumbers As Long, _
        ByVal includeSymbols As Long, Optional _
        ByVal includeDate As Boolean, _
        Optional ByVal myDate As String = vbNullString) As String
    Dim s As String, p As String, t As String, symbols As String, n As Long, i As Long, j As Long
    If includeCapitalLetters < 0 Or includeSmallLetters < 0 Or includeNumbers < 0 Or includeSymbols < 0 Or _
            includeCapitalLetters + includeSmallLetters + includeNumbers = 0 Or includeDate And myDate = "" Then
        GenerateRandomString = "-- Error --"
        Exit Function
    End If
    symbols = "!@#$%^&*()_-+={}[]|\:;""'<>,.?/"
    For i = 1 To includeCapitalLetters
        n = Application.RandBetween(65, 90)
        s = s & Chr(n)
    Next i
    For i = 1 To includeSmallLetters
        n = Application.RandBetween(97, 122)
        s = s & Chr(n)
    Next i
    For i = 1 To includeNumbers
        n = Application.RandBetween(48, 57)
        s = s & Chr(n)
    Next i
    For i = 1 To includeSymbols
        n = Application.RandBetween(1, Len(symbols))
        s = s & Mid(symbols, n, 1)
    Next i
    For i = Len(s) To 2 Step -1
        j = Application.RandBetween(1, i - 1)
        p = Mid(s, i, 1)
        Mid(s, i, 1) = Mid(s, j, 1)
        Mid(s, j, 1) = p
    Next i
    If InStr(symbols, Left(s, 1)) > 0 Then
        For j = 2 To Len(s)
            If InStr(symbols, Mid(s, j, 1)) = 0 Then Exit For
        Next j
        p = Left(s, 1)
        Mid(s, 1, 1) = Mid(s, j, 1)
        Mid(s, j, 1) = p
    End If
    If includeDate Then
        t = "-" & Format(IIf(myDate = "", Date, myDate), "ddmmyyyy")
        s = s & t
    End If
    GenerateRandomString = s
End Function
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Generate random string using UDF

Post by YasserKhalil »

Amazing. Thank you very much.

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: Generate random string using UDF

Post by SpeakEasy »

It is possible there may be a better way to do this is we knew the purpose of the random string

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Generate random string using UDF

Post by YasserKhalil »

The purpose is that I need random passwords but in specific patterns.

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Generate random string using UDF

Post by StuartR »

Most password managers include a random password generator. If you're not using a password manager then maybe now is the time to start
StuartR


YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Generate random string using UDF

Post by YasserKhalil »

Thanks a lot for the advice.

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: Generate random string using UDF

Post by SpeakEasy »

>random ... specific patterns

These two are not compatible ...

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Generate random string using UDF

Post by YasserKhalil »

Why are not compatible?
Random in the output for capital letters and small letters and numbers and symbols. But the pattern would be the same (example: 3 capital letters , 2 small letters, 1 symbol, 4 numbers). This is what I meant with the specific pattern.

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: Generate random string using UDF

Post by SpeakEasy »

By introducing patterns you reduce randomness/entropy
"Enforcing pattern rules essentially reduces the randomness (i.e. entropy) of the password by limiting the number of possibilities."

"Enforcing pattern rules essentially divulges information to a potential attacker about the passwords"
        Password.org
In your example case above you have reduced the bits of entropy from a potential 65.7 to 42.11


So why do you need the patterns?

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Generate random string using UDF

Post by YasserKhalil »

Because this was a request from my boss. That's all.

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: Generate random string using UDF

Post by SpeakEasy »

Boss probably doesn't know that this reduces password strength