#!/usr/bin/perl -w
# Calculate every combination of any given string
# Author: B.J. van Buuren <bvbuuren@bigfoot.com>
# License: GPLv2
# $Id: bruteforce.pl,v 1.9 2004/09/13 17:45:57 jacco Exp $
use strict;
use vars qw(@ARGV);

my($SCRIPT) = $0;

sub usage
{
	print("Syntax: $SCRIPT number-of-chars [string-to-choose-from]\nExample: $SCRIPT 3 abc\n");
	return;
}

my($ABC) = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ |[]\\\'\`!\@\#\$\%\^\&\*()-_=+{}\":;.,<>\?\/\~";

if ( $#ARGV < 0 ) {
	usage;
	exit;
}
my($COUNT) = $ARGV[0];
if ( $COUNT =~ /\D+/ ) {
	usage;
	die "\"$COUNT\" is not numeric.\n";
}
$ABC = $ARGV[1] if ( defined($ARGV[1]) );

my($I,$K,$LENGTH) = 0;
my($e,$f,$j) = 0;
my($c) = '';

$LENGTH = length($ABC);

$I = $COUNT - 1;

$K = $LENGTH ** $COUNT;

for($j=0; $j<$K; ++$j) {
	print("$j : ");
	for($f=$I; $f>=0; --$f) {
		$e = int($j/($LENGTH**$f)) % $LENGTH;
		$c = substr($ABC, $e, 1);
		print("$c");
	}
	print("\n");
}

