Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: function problem in password generator

  1. #11
    Senior Member
    Join Date
    Feb 2004
    Location
    Near Manchester (England)
    Posts
    145

    Lightbulb I See You Have found Your Problem ...

    Just thought I'd suggest the following:

    Code:
    By lepricaun
    
    void generate_password(length)
    {
        int length;
        srand(time(NULL));    
        while (length>0)
        {
            
            int cha;
            cha=rand()%128;
            if((cha>=48 && cha<=57) || (cha>=65 && cha<=90) || (cha>=97 && cha<=122))
            {
               printf("%c",cha);
               length--;
            }
        }
    }
    Yes this works, however, should you be declaring the cha variable every time you loop round in the while loop? It's not efficient. 9/10 from me, especially as you didn't do that in the code this superceeded! LOL
    Tomorrow is another day for yesterdays work!

  2. #12
    yes, your right, but sorry mate, you're a little behind...

    this is an old thread, and old code too

    here's the final version of the password generator:
    pwgenCL_2

    Code:
    /*
    pwgenCL version 2 (pwgenCL_2)Copyright (C) 2004 Scorpius, password generator, 
    written as a command line scripting utility.
    this program is freesource, which means you may use and/or modify it under the GPL license.
    if you'd like to contact me with comments/bugs/suggestions or just to say hello,
    my email address is: [email protected].
      
    i hope you enjoy using my program and that you find it usefull :)         
      
    to compile it under windows, get a copy of Dev-C++ (it's free), or another C/C++ compiler.
    to compile it under linux, just type "gcc -o pwgenCL_2 pwgenCL_2.c" (without the " quotes)
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <strings.h>
    
    char *generated_password(char*, int,int);
    
    int main(int argc, char** argv)
    {   /*the character sets*/
        char *very_easy_set = "abcdefghijklmnopqrstuvwxyz";
        char *very_easy2_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char *easy_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char *medium_set = "abcdefghijklmnopqrstuvwxyz0123456789";
        char *hard_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        char *hard2_set = "\\][^_abcdefghijklmnopqrstuvwxyz0123456789";
        char *very_hard_set = "!\"#$%'(*)+,-./abcdefghijklmnopqrstuvwxyz0123456789";
        char *extreme_set = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
        
        int characterset=0;
        int password_length=0;
        int user_supplied=0;
        if(argc!=4)
        {
    	    printf("\npwgenCL_2,commandline password generator, Copyright (C) 2004 Scorpius");
        	printf("\n\npwgenCL_2 is free software; you can redistribute it and/or modify");
        	printf("\nit under the terms of the GNU General Public License as published by");
        	printf("\nthe Free Software Foundation; either version 2 of the License.");
        	printf("\n\npwgenCL_2 is distributed in the hope that it will be useful,");
        	printf("\nbut WITHOUT ANY WARRANTY; without even the implied warranty of");
        	printf("\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the");
        	printf("\nGNU General Public License for more details.");
        	printf("\n\nYou should have received a copy of the GNU General Public License");
        	printf("\nalong with this program; if not, write to the Free Software");
        	printf("\nFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n");
        	printf("\n\t   ***Contact Scorpius: [email protected]***");
        	printf("\n\nCharacter sets:");
        	printf("\n1: (a-z)\t\t\t\t\tvery easy password");
            printf("\n2: (A-Z)\t\t\t\t\tvery easy password");
            printf("\n3: (a-z,A-Z)\t\t\t\t\teasy password");
            printf("\n4: (a-z,0-9)\t\t\t\t\tmedium password");
            printf("\n5: (A-Z,a-z,0-9)\t\t\t\thard password");
            printf("\n6: (a-z,0-9, \\ ] [ ^ _ )\t\t\thard password");
            printf("\n7: ( ! \" # $ % ' ( * ) + , - . /  ,a-z,0-9)\tvery hard password");
            printf("\n8: full keyboard characters (except space), almost impossible to crack\n\n");
            printf("The maximum password length is 256 characters\n");
            printf("Usage: %s <characterset> <password length> <user supplied number>\n",argv[0]);
            printf("\n*The <user supplied number> is inserted to create extra randomness*\n");
            printf("\nExample: %s 8 15 5454303\n",argv[0]);
            return 0;
        }
        if(argc==4)/*if all parameters, (characterset,passwordlength,randomization number) run the program*/
        {
        	characterset=atoi(argv[1]);/*create integer from the argument*/
        	password_length=atoi(argv[2]);/*create integer from the argument*/
        	user_supplied=atoi(argv[3]);/*create integer from the argument*/
        	if (password_length<1)
            {
    		printf("Password length: %d is too small.\n",password_length);
    		return EXIT_FAILURE;
    		}
    		if (password_length>256)
    		{
    		printf("Password length: %d is too large.\n",password_length);
    		return EXIT_FAILURE;
    		}
        	switch(characterset)
        	{
                    case 1:
                            generated_password(very_easy_set,password_length,user_supplied);
                            break;
                    case 2:
                            generated_password(very_easy2_set,password_length,user_supplied);
                            break;
                    case 3:
                            generated_password(easy_set,password_length,user_supplied);
                            break;
                    case 4:
                            generated_password(medium_set,password_length,user_supplied);
                            break;
                    case 5: 
                            generated_password(hard_set,password_length,user_supplied);
                            break;
                    case 6:
                            generated_password(hard2_set,password_length,user_supplied);
                            break;
                    case 7:
                            generated_password(very_hard_set,password_length,user_supplied);
                            break;
                    case 8:
                            generated_password(extreme_set,password_length,user_supplied);
                            break;
                   default:
                            printf("Invalid character set\n");
    			            return EXIT_FAILURE;
                            break;
        	}
    	    return EXIT_SUCCESS;
        }   
        else return EXIT_FAILURE;  
    }
    
    /*the password generator function*/
    
    char *generated_password(char* char_set, int length,int user_number)
    {
            
            srand(user_number*time(NULL));
            int loop;
            int set_length=strlen(char_set);
        
            for(loop=0;loop<length;loop++)
            {
                 putchar(char_set[rand()%set_length]);
            }
            printf("\n");
    }
    and as you can see, the declaration of the variable isn't done in the loop anymore

    for more info on this program check this thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •