[VB.Net] Générateur de mots

word generator

Pour la réalisation d’un nouvel outil de sécurité j’ai dû généré un dictionnaire de mots allant de 3 à 8 lettres.
Pour faire çà, j’ai donc développé la classe suivante:

'Exemple: Fonction qui génère les fichiers de words de 5 lettres
Private Sub Generator()
Call WordGenerator.Start(5)
Dim aLine As String = WordGenerator.GetString()
Dim tmp As New List(Of String)
While aLine IsNot Nothing
tmp.Add(aLine)
FileCounter += 1
If FileCounter = FileMax Then
Dim sw As New StreamWriter(Path.Combine("files", Environment.TickCount.ToString), False, System.Text.Encoding.UTF8)
For i As Integer = 0 To tmp.Count - 1
sw.WriteLine(tmp(i))
Next
sw.Close()
tmp.Clear()
FileCounter = 0
End If
aLine = WordGenerator.GetString()
End While
End Sub
'Classe qui génère des words
Public Class WordGenerator
Private Const _charStart As Integer = 33 'Code ASCII de la premiere lettre
Private Const _charEnd As Integer = 126 'Code ASCII de la dernière lettre
Public Shared Started As Boolean = False
Private Shared Arr() As Integer
Public Shared Sub Start(ByVal num As Integer)
ReDim Arr(num - 1)
For i As Integer = 0 To num - 1
Arr(i) = _charStart
Next
Started = True
End Sub
Public Shared Function GetString(Optional ByVal idx As Integer = 0) As String
Dim ret As String = Nothing
If Started Then
Arr(idx) = Arr(idx) + 1
If Arr(idx) > _charEnd Then
Arr(idx) = _charStart
If idx + 1 < Arr.Length Then
ret = GetString(idx + 1)
End If
Else
For i As Integer = 0 To Arr.Length - 1
ret &= Chr(Arr(i))
Next
End If
End If
Return ret
End Function
End Class

Vous générerez rapidement plusieurs Giga de mots uniques. Par exemple, pour 5 lettres, vous génèrerez 10000 millions de mots uniques

 

Leave a Reply

Your email address will not be published. Required fields are marked *