Return to Snippet

Revision: 37881
at December 19, 2010 00:57 by Affix


Initial Code
//
// Author : Keiran "Affix" Smith <Affix_at_Affix_dot_me>
// Website: http://keiran-smith.net
// Description :
// Generate a Simple Random Password
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

#include <stdio.h>
#include <stdlib.h>
#ifdef DEVRANDOM
	#include <fcntl.h>
#endif


int max;
char *charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

void seed()
{
        int i, j;
#ifdef DEVRANDOM
        int fd;
#endif
        i = getpid();
        j = time(NULL) ^ (i + (i << 15)); /* Thank you the Camel Book */
#ifdef DEVRANDOM
        fd = open(DEVRANDOM, O_RDONLY, 0);
        if (fd == -1) {
                perror(DEVRANDOM);
                exit(1);
        }
        read(fd, &i, sizeof(charset));
        j = j ^ i;
        close(fd);
#endif
        srand(j);
}

char *shuffle(char *v, size_t n)
{
        int i, j;
        char *s;

        for (i=0; i < n; i++) {
                do
                        j = rand() % n;
                while (j < i);
                s = v;
                v = v;
                v = s;
        }

	return v;
}



int main(int argc, char *argv[])
{
	
	if(argv[1])
		max = atoi(argv[1]);
	else
		max = 8;
	printf("Maximum Password Length is : %d\n\n", max);
	
	seed();

	int i = 0;
	while(i < max)
	{
		charset = shuffle(charset, sizeof(charset));
		int rnd_num = rand() % sizeof(charset);
		char pchar = charset[rnd_num];
		printf("%c", pchar);
		i++;
	}	
	printf("\n\n");
	return 0;
}


Initial URL

                                

Initial Description

                                

Initial Title
Simple Random Password Generator

Initial Tags

                                

Initial Language
C