/* This is a C implementation of the gen.pl script I wrote some time ago.
 * I needed it in C for an assignment involving brute forcing password hashes.
 * In this case it is implemented in a loop that does lengths 1-8.
 * The same could easily be done in the Perl script.
 * The gen() methods in C and Perl are functionally equivalent.
 */
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
void gen(char *word, char *chars, int ug, int length);
 
int main(int argc, char* argv[]) {
 
	/* Set of characters to include (\ and " need to be escaped)
	 * In this case we needed the full keyspace, but a subset may be used.
	 */
	char *chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_=+{}[]|/\\;:'\",.<>?";
 
	/* Start at 1 char long */
	int i = 1;
 
	/* End at 8 chars long */
	int length = 8;
 
	/* Allocate memory for the output */
	char *word = calloc(length, 0);
 
	/* The gen() function only generates a set for a specified length
	 * so when we need all we put it in a loop
	 */
	while(i <= length) {
		gen(word, chars, 0, i++);
	}
 
	return 0;
}
 
/* Function to generate all possible itterations */
void gen(char *word, char *chars, int ug, int length) {
	size_t i = 0;
	int u = ug;
	while(i < strlen(chars)){
		word[u] = chars[i++];
 
		if(u < (length-1)) {
			gen(word, chars, ++u, length);
			u--;
		} else {
			printf("%s\n", word);
		}
	}
}