Published by
Jan 29, 2012 (last update: Jan 30, 2012)

Password Creator (In C)

Score: 3.6/5 (154 votes)
*****
I was reading this and got the urge to write a program in C so I decided to rewrite the password creator in C.

I also forgot to give credit for the inspiration and some of the code
here is the where I got my idea sorry Catfish for forgetting won't happen again promise.

This is my first time really using C so I am sorry for any mistakes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

//Parser.h

#ifndef _PARSER_H_
#define _PARSER_H_

#include <ctype.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NO_FILE -1
#define NO_PASSWORD -1
#define NO_POS -1

int getLast (char* , char);

int getFilePos (char** , int);
int getPasswordSize (char** , int);

int getFile (char** , char* , int);

#endif /* _PARSER_H_ */


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

//Parser.c

#include "Parser.h"

int getLast (char* String , char Char)
{
    //Iterate and get the last instance of the character
    int i = 0;
    int Pos = 0;

    for (i = 0 ; i < strlen (String) ; ++i)
    {
	//Check if current character is the one we are looking for
	if (String [i] == Char)
	{   
	    //Assign to the current position
	    Pos = i;
	}
    }
    
    //Check if we found the character
    if (Pos == 0)
    {
	return NO_POS;
    }    
    else
    {
	return ++Pos;
    }
}

int getFilePos (char** Arguments , int Size)
{
    //Iterate and check whether there are any dashes
    int i = 0;
    int j = 0;
    
    for (i = 0 ; i < Size ; ++i)
    {
	for (j = 0 ; j < strlen (Arguments [i]) ; ++j)
	{
	    //Check if there is any dashes
	    if (Arguments [i][j] == '-')
	    {	
		//Return the string that has the dashes		
		return i;
	    }
	}
    }
    
    return NO_FILE;
}

int getPasswordSize (char** Arguments , int Size)
{
    //Iterate
    int i = 0;
    int j = 0;
    
    for (i = 0 ; i < Size ; ++i)
    {
	//Temp variable
	int Temp = 0;

	for (j = 0 ; j < strlen (Arguments [i]) ; ++j)
	{
	    //If this is not a number then break
	    if (!isdigit (Arguments [i][j]))
	    {
		break;
	    }
	    //Otherwise increment a temp variable
	    else
	    {
		Temp++;
	    }
	}

	//All of the characters in the current string were numbers
	if (Temp == strlen (Arguments [i]))
	{
	    //Return the integer value
	    return atoi (Arguments [i]);
	}
    }
    
    return NO_PASSWORD;
}



int getFile (char** Arguments , char* Buffer , int Size)
{
    //Check if the file exists
    if (getFilePos (Arguments , Size) == NO_FILE)
    {
	return NO_FILE;
    }
    else
    {
	//String containing the file argument
	char* File = Arguments [getFilePos (Arguments , Size)];

	//Copy the rest of the string
	strcpy (Buffer , File + getLast (File , '-'));
	
	//Everything is okay
	return 0;
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

//Generator.h

#ifndef _GENERATOR_H_
#define _GENERATOR_H_

#include <time.h>
#include <stdlib.h>

#include "Parser.h"

#define MAX_PASSWORD_SIZE 500

char randomChar ();

void appendChar (char* , char);
void printPassword (char** , int);

int genPassword (char** , char* , int);

#endif /* _GENERATOR_H_ */


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

//Generator.c

#include "Generator.h"

char randomChar ()
{
    char Chars [] = 
	"abcdefghijkmnlopqrstuvwxz"
	"ABCDEFGHIJKMNLOPQRSTUVWXYZ"
	"1234567890";
    
    return Chars [rand () % strlen (Chars)];
}

void appendChar (char* Buffer , char Char)
{
    //Buffer size
    int Size = strlen (Buffer);

    //Add the character and the null character
    Buffer [Size] = Char;
}

int genPassword (char** Arguments , char* Buffer , int Size)
{
    //Password Size
    int PasswordSize = getPasswordSize (Arguments , Size);

    //Check if the size is valid
    if (PasswordSize == NO_PASSWORD)
    {
	return NO_PASSWORD;
    }
    if (PasswordSize > MAX_PASSWORD_SIZE)
    {
	return NO_PASSWORD;
    }

    //Iterate and append a character the end of the string
    int i = 0;
    
    for (i = 0 ; i  < PasswordSize ; ++i)
    {
	appendChar (Buffer , randomChar ());
    }
    
    return 0;
}

void printPassword (char** Arguments , int Size)
{
    char Buffer [FILENAME_MAX] = "";
    
    //Check if we could generate a password
    if (genPassword (Arguments , Buffer , Size) == NO_PASSWORD)
    {
	fprintf (stderr , "Invalid password size!! \n");
	return;
    }

    //Check where we are printing to
    if (getFilePos (Arguments , Size) == NO_FILE)
    {
	fprintf (stdout , "%s \n" , Buffer);
    }
    else
    {
	//File path
	char FilePath [FILENAME_MAX] = "";

	//Generate the path
	getFile (Arguments , FilePath , Size);

	//Stream to print to
	FILE* Stream = fopen (FilePath , "a");

	//Check if the stream is valid
	if (Stream == NULL)
	{
	    return;
	}
	//We can write to the file
	else
	{	    
	    //Print it
	    fprintf (Stream , "%s \n" , Buffer);
	    
	    //Close the stream
	    fclose (Stream);
	}
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

//main.c

#include <stdio.h>

#include "Parser.h"
#include "Generator.h"

int main(int argc , char* argv[])
{
    //Check if there are any arguments
    if (argc < 2)
    {
	//Print out usage
	fprintf (stderr , "Usage: %s --File PasswordSize \n \n" , argv [0]);
	fprintf (stderr , "Examples:\n%s --Passwords.txt 34 \n" , argv [0]);
	fprintf (stderr , "%s 7 \n" , argv [0]);
    
        //There was an error!!
	return 1;
    }

    //Seed the generator
    srand ((unsigned) time (NULL));

    //Print password
    printPassword (argv , argc);

    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#
# Makefile for 'Creator'.
#
# Type 'make' or 'make Creator' to create the binary.
# Type 'make clean' or 'make clear' to delete all temporaries.
# Type 'make run' to execute the binary.
# Type 'make debug' to debug the binary using gdb(1).
#

# build target specs
CC = gcc
CFLAGS = -Wall -g 
OUT_DIR = release_build
LIBS =

# first target entry is the target invoked when typing 'make'
default: Creator

Creator: $(OUT_DIR)/Generator.c.o $(OUT_DIR)/main.c.o $(OUT_DIR)/Parser.c.o
	@echo -n 'Linking Creator... '
	@$(CC) $(CFLAGS) -o Creator $(OUT_DIR)/Generator.c.o $(OUT_DIR)/main.c.o $(OUT_DIR)/Parser.c.o $(LIBS)
	@echo Done.

$(OUT_DIR)/Generator.c.o: Generator.c Generator.h Parser.h
	@echo -n 'Compiling Generator.c... '
	@$(CC) $(CFLAGS) -o $(OUT_DIR)/Generator.c.o -c Generator.c
	@echo Done.

$(OUT_DIR)/main.c.o: main.c Parser.h Generator.h
	@echo -n 'Compiling main.c... '
	@$(CC) $(CFLAGS) -o $(OUT_DIR)/main.c.o -c main.c
	@echo Done.

$(OUT_DIR)/Parser.c.o: Parser.c Parser.h
	@echo -n 'Compiling Parser.c... '
	@$(CC) $(CFLAGS) -o $(OUT_DIR)/Parser.c.o -c Parser.c
	@echo Done.

run:
	./Creator 

debug:
	gdb ./Creator

clean:
	@echo -n 'Removing all temporary binaries... '
	@rm -f Creator $(OUT_DIR)/*.o
	@echo Done.

clear:
	@echo -n 'Removing all temporary binaries... '
	@rm -f Creator $(OUT_DIR)/*.o
	@echo Done.
 */