''' <summary>
'' Cette fonction crée un mot de passe de la longueur désirée
'' avec les caractères choisis
'' </summary>
'' <param name="Length">Taille désirée</param>
'' <param name="CLower">Alphabet minuscule</param>
'' <param name="CUpper">Alphabet MAJUSCULE</param>
'' <param name="CNum">Numéros</param>
'' <param name="CPerso">série de caractères qui peut être fournie</param>
'' <returns></returns>
'' <remarks>
'' voir la fonction exemple pour l'utilisation
'' </remarks>
Public Function CreateRandomPassword(ByVal Length As Integer, ByVal CLower As Boolean, ByVal CUpper As Boolean, ByVal CNum As Boolean, Optional ByVal CPerso As String = vbNullString) As String
Dim minus As String = "abcdefghijkmnopqrstuvwxyz"
Dim majus As String = "ABCDEFGHJKLMNOPQRSTUVWXYZ"
Dim numb As String = "0123456789"
Dim _allowedChars As String = String.Empty
If CLower Then
_allowedChars = _allowedChars & minus
End If
If CUpper Then
_allowedChars = _allowedChars & majus
End If
If CNum Then
_allowedChars = _allowedChars & numb
End If
_allowedChars = _allowedChars & CPerso
Dim randNum As New Random()
Dim chars(Length - 1) As Char
Dim allowedCharCount As Integer = _allowedChars.Length
For i As Integer = 0 To Length - 1
chars(i) = _allowedChars.Chars(CInt(Fix((_allowedChars.Length) * randNum.NextDouble())))
Next i
Return New String(chars)
randNum = Nothing
End Function
''' <summary>
''' Exemple d'utilisation qui genere
''' 100 mots de passe et qui en choisit 1 seul au hasard
''' </summary>
''' <returns></returns>
Function Exemple() As String
Dim tab(100) As String
Dim x As Integer
Dim r As New Random
For x = 0 To 100
tab(x) = CreateRandomPassword(10, True, True, True, "&|@#§^!°-_$%=+~<>.,;")
Next
Return tab(r.Next(0, 99))
End Function