Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Brute Force Algorithm with Perl reg exprs?

by sfink (Deacon)
on Apr 25, 2006 at 02:57 UTC ( [id://545450]=note: print w/replies, xml ) Need Help??


in reply to Brute Force Algorithm with Perl reg exprs?

Rethink your algorithm. It sounds like you're trying to do it bottom-up, making a fancy decision at each position for what should go into that position. What if instead you separated the problem into two pieces: the position in the password where the hint appears, and the remaining non-hint characters? Those two together can be processed to produce a unique password.

Regexps are more for going the other way around: given this password, separate it into the hint and other characters. Sure, you could use substitution to produce a password from a hint pattern:

$other = "x!4Y"; $password = $PATTERN; # $PATTERN is aHINTbcd $password =~ s/a/x/; # or $password =~ s/a/substr($other,0,1)/e; $password =~ s/b/!/; $password =~ s/c/4/; $password =~ s/d/Y/;
but the regular expressions aren't doing you any favors. tr/// would do it in one step:
$other = "x!4Y"; $password = $PATTERN; # $PATTERN is aHINTbcd $password =~ tr/abcd/$other/;
but that's still far more complicated than just tacking the pieces together:
$password = substr($other, 0, 1).$HINT.substr($other, 1, 3);
and less flexible than using join():
$password = join('', substr($other, 0, 1), $HINT, substr($other, 1, +3));
which can be easily parameterized to reposition the hint within the password.

Yes, regexps implicitly do looping, so it is possible to do the entire thing with a "single" (ir)regexp, but it would be a mess, and much harder to write than straightforward code. (I put "single" in quotes because the body of the regexp would contain at least one code block with multiple statements.)

More likely, you could generate every possible full-length password and use a regexp to filter out the ones containing the hint -- but that rather defeats the purpose of having a hint.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://545450]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 05:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found