delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
498 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Threaded Brute Forcing ClassFormat this article printer-friendly!Bookmark function is only available for registered users!
Nice Threaded Brute Forcing Class
Product:
Delphi all versions
Category:
Algorithm
Skill Level:
Scoring:
Last Update:
08/21/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet brute brute-force thread threaded class algorithm combination
Times Scored:
1
Visits:
2812
Uploader: Stewart Moss
Company: New Heights Software Developme
Reference: http://www.new-heights.co.za/Delphi/Components
 
Question/Problem/Abstract:
How to create a simple brute forcing engine in a delphi class.
Answer:




{-----------------------------------------------------------------------------
Unit Name: classThreadBruteForce

Version: 1.0

Release Date: 21-Aug-2002

Compiler directives:

    TINY     - removes unnessecary error messages. test that output
               is not empty
    and
    OPTIMIZE (less information is available)

Purpose:

Description:

    A TThread which generates brute force combinations through the
    onDo event.

Notes:
    Charset contains the characters (these are sorted internally)
    onFinished event provided


    Not exactly fast but it does the job.


    Use it like this:-

    brute := TBruteThread.create(true);
    brute.charset := 'abcdefghijklmnopqrstuvwxyz';  // Chars to brute
    brute.numCharacters := 5;                       // Max chars
    brute.onDo := Form1ThreadOnDo;
    brute.resume;


Dependancies:

History:

        Copyright 2002 by Stewart Moss
        All rights reserved.
-----------------------------------------------------------------------------}

unit classBruteForce;

interface
uses
classes, sysutils;

type
  
TBruteThread = class(TThread)
  
private
    
FNumChars: Integer;
    
FCharset: string;
    
FonDo: TNotifyEvent;
    
FonFinished: TNotifyEvent;

    
CharCount: string;
    
minChar: char;
    
maxChar: char;
    
imaxChar: integer;

    
incBruteLock: boolean;
    
// locks the incBrute function

    
procedure init;
    
function incBrute(posi: integer): integer;
    
function StringBubbleSort(StrIn: string): string;

  
public
{$IFNDEF OPTIMIZE}
    
BruteCount: integer; // not recommended on large bruteforce,
                         // use your own counter
{$ENDIF}
    
BruteResult: string;

    
procedure execute; override;
    
    

  
published

    property
onDo: TNotifyEvent read FonDo write FonDo;
    
property onFinished: TNotifyEvent read FonFinished write FonFinished;

    
property CharSet: string read FCharset write FCharset;
    
property numCharacters: Integer read FNumChars write FNumChars;

  
end;

implementation

{ TBruteThread }

procedure TBruteThread.execute;
var
  
loop: integer;
  
tmpstr: string;
begin
  if
FNumChars <= 0 then
  begin
{$IFNDEF TINY}
    
raise exception.create('invalid Numchars');
{$ENDIF}
{$IFDEF TINY}
    
exit;
{$ENDIF}
  
end;

  
if FCharSet = '' then
  begin
{$IFNDEF TINY}
    
raise exception.create('Charset is blank');
{$ENDIF}
{$IFDEF TINY}
    
exit;
{$ENDIF}
  
end;

  
init;

  
while (not terminated) do
  begin
    if
incbrute(1) > FNumChars then
      
break;

    
loop := 0;
    
bruteresult := '';
    
while loop < FNumChars do
    begin
      
inc(loop);
      
if charcount[loop] = #0 then break;

      
// speed optimization
      
tmpstr := BruteResult;
      
BruteResult := tmpstr + charcount[loop];
    
end;
{$IFNDEF OPTIMIZE}
    
inc(Brutecount);
{$ENDIF}

    
if assigned(onDo) then
      
onDo(Self);
  
end;

  
if assigned(onFinished) then
    
onFinished(Self);
end;

{-----------------------------------------------------------------------------
  Procedure: incBrute
  Arguments: posi: integer
  Result:    integer

  Purpose: Recurive

  Description:
    This function brutes

      Copyright 2002 by Stewart Moss
      All rights reserved.
-----------------------------------------------------------------------------}
function TBruteThread.incBrute(posi: integer): integer;
var
  
tmpint: integer;
  
bufferpos: integer;
begin
  
result := posi;
  
bufferpos := pos(charcount[posi], FCharset);
  
charcount[posi] := FCharset[bufferpos + 1];
  
if FCharset[Bufferpos] = maxchar then
  begin
    
charcount[posi] := minchar;
    
tmpint := incBrute(posi + 1);
    
if tmpint > FnumChars then
      
result := tmpint;
  
end;
end;

procedure TBruteThread.init;
var
  
loop: integer;
begin
  
FCharSet := StringBubbleSort(FCharset);
  
minchar := FCharset[1];
  
maxChar := FCharset[length(FCharset)];
  
imaxchar := ord(MaxChar);
  
charcount := '';
  
for loop := 1 to FNumChars do
  begin
    
charcount := charcount + #0;
  
end;
{$IFNDEF OPTIMIZE}
  
Brutecount := 0;
{$ENDIF}

end;

function TBruteThread.StringBubbleSort(StrIn: string): string;
var
  
i, j: Integer;
  
temp: Char;
  
tmplen: integer;
begin
  
tmplen := length(StrIn);
  
for i := 1 to tmplen do
    for
j := 1 to tmplen do
      if
strIn[i] < StrIn[j] then
      begin
        
temp := StrIn[i];
        
StrIn[i] := StrIn[j];
        
StrIn[j] := temp;
      
end;
  
Result := strIn;
end;

end.







Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
inner loop optimization
    Andreas Schmidt (Aug 22 2002 7:06PM)

This loop runs much faster than your orginal version:

    loop := 0;
    SetLength(bruteresult, FNumChars);
    while loop < FNumChars do
    begin
      inc(loop);
      if charcount[loop] = #0 then break;

      BruteResult[loop] := charcount[loop];
    end;
    SetLength(BruteResult, loop);

see
http://www.optimalcode.com/string.htm#prealloc
for more info about string optimizations.
Respond

RE: inner loop optimization
Stewart Moss (Aug 23 2002 8:30AM)

Nice one :)

Thank you very much :) :)
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
R. Lefter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)