FreePascal implementation of the Brute Force Algorithm
This is my single threaded implementation of the Brute Force algorithm for FreePascal / Delphi. I transferred this algorithm from Java. The Java algorithm can be found here
program Pascal_BruteForce; (* Brute Force Algorithm for FreePascal Author: Nikos Siatras https://github.com/nsiatras *) uses SysUtils; // Global Variables var fCharactersStr: string; fCharList: TStringArray; fCharactersListLength: integer; {* The OnBruteForceWordGenerated procedure is called everytime a new word is generated from the BruteForce algorithm For this example I choose only to write to word on the screen *} procedure OnBruteForceWordGenerated(word: string); begin WriteLn(word); end; (* Change characters to each other and generate BruteForce words :) *) procedure ChangeCharacter(pos: integer; sb: array of string; wordLength: integer); var i, x: integer; generatedWord: string; begin for i := 0 to fCharactersListLength - 1 do begin sb[pos] := fCharList[i]; if pos = wordLength - 1 then begin // Pass the BruteForce generated word to the OnBruteForceWordGenerated // procedure generatedWord := string.Join('', sb); OnBruteForceWordGenerated(generatedWord); end else begin ChangeCharacter(pos + 1, sb, wordLength); end; end; end; (* Start Brute Force Generation @wordLength is the word's length (how many characters a word can have) *) procedure StartBruteForce(wordLength: integer); var stringBuilder: array of string; currentChar: string; i: integer; begin SetLength(stringBuilder, wordLength); currentChar := fCharList[0]; for i := 1 to wordLength do begin stringBuilder[i - 1] := currentChar; end; ChangeCharacter(0, stringBuilder, wordLength); end; // Main Program var i: integer; begin //Initialize fCharList array //For this example I will use all lowercase english alphabet characters. fCharactersStr := 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'; fCharList := fCharactersStr.split(','); //Find the length of the fCharList array fCharactersListLength := Length(fCharList); // Generate all words with total length from 1 to 16 characters // This will generate all words from 'a' to 'zzzzzzzzzzzzzzzz' for i := 1 to 16 do begin StartBruteForce(i); end; end.
0 comments:
Post a Comment