You mentioned you wanted to recursion. Recursion (or more specifically, a stack) is the tool of choice to use if you want to do a depth-first search. However, a depth-first search is not optimal here, since people usually choose passwords much smaller then the maximum length. For example,

# Depth-first search # Max length = 20 1234 1234a 1234aa 1234aaa 1234aaaa ... 1234aaaaaaaaaaaaaaaa 1234b 1234bb ...

A breadth-first search would be more appropriate. For example,

# Breadth-first search 1234 1234a 1234b 1234c ... 12348 12349 1234aa 1234ab 1234ac ...

Breadth-first search uses a queue, not a stack, so recursion is not appropriate. Regexps are also inappropriate because they strength is in backtracking, which is done using stacks. So let's look at a breadth-first solution:

use strict; use warnings; sub crack { my $hint = shift; my $target = shift; my $salt = substr($target, 0, 2); my @chars = @_; my @to_process = ( '' ); my $passwd; while (@to_process) { my $pad = shift(@to_process); push(@to_process, $pad . $_) foreach @chars; for my $pos (0..length($pad)) { $passwd = $pad; substr($passwd, $pos, 0, $hint); return $passwd if crypt($passwd, $salt) eq $target; } } } my @chars = ('a'..'z', 'A'..'Z', '0'..'9'); print crack('12', crypt('d12e', 'AA'), @chars), "\n"; print crack('1234', crypt('aa12345', 'AA'), @chars), "\n";

Unfortunately, the above takes way too much memory. We need to find an interator which generates the next item to process on the fly. Since the to-process list is a simple incrementing sequence, it's not only possible to generate the next item on the fly, it's rather simple. The following code performs a breadth-first search using virtually no stack or heap memory:

Working solution withheld for a week, since you said this was for class.


In reply to Re: Brute Force Algorithm with Perl reg exprs? by ikegami
in thread Brute Force Algorithm with Perl reg exprs? by scotchfx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.