Tuesday, August 23, 2011

Unique Characters from Crunch Redux

This ended up on TAPE's blog as a main page link, so this is me cleaning it up a bit. The code here will give you a filtered wordlist that contains completely unique character lines. Which means that a line of "abc" will get through, but a line of "aabc" will not (double a's). Further, "abca" will not be permitted either. The code works on a relatively simple bit of math - if the count of unique characters matches the length of the line, print the line out.
#!/usr/bin/env perl

while(my $line = <>) {
    my %unique;
    my $size = grep { !$unique{$_}++ } split //, $line;
    print "$line" if length($line) == $size;
}

If you would prefer to show lines that have one character difference, you can replace the print statement with this:
my $ok = 0;
foreach my $current (keys %unique) {
    if ( $unique{$current} > 2 ) { $ok = 1; }
}
print "$line" if $ok == 0;
This will permit "aabc" and "abca" as well as "aabb" and the like. It should be easy for someone to figure out from there ;)

3 comments:

Arthur said...

Advice for others:
To run script 10% faster try Speedy. So on ubuntu(or any Backtrack) system type:
apt-get install speedy-cgi-perl
and change first line to;
#!/usr/bin/speedy
On i7 system can run 5 proccess same time to speedup work.

Gitsnik said...

The fuck!?

What part of the script looks like it runs CGI under a web server??

Real advice: Ignore previous advice.

arthur said...

Maybe you are right, but it works and do same job. and its faster.
so sorry if any...