PASSWORD GENERATOR
CHARSETS.H
/*######################################################################################
# Dev: cnd.dev
# Program Name: N/A
# Version: 1.0.0
# - Major.Minor.Update/BuildNumber
# Date: 231504JUN25
# Filename: charsets.h
# Dependency: N/A
# Compile Cmd: N/A
# Synopsis:
# - Overview: Declares functions for populating arrays with specific character sets
# (uppercase, lowercase, digits, symbols) using ASCII values.
# - Technical: Intended for programs that need predefined character arrays, such as
# password generators or data format validators.
######################################################################################*/
#ifndef CHARSETS_H
#define CHARSETS_H
typedef struct{
char uppercaseLetters[27];
char lowercaseLetters[27];
char numbers[10];
char symbols[33];
char randomPassword[255];
char collection[255];
} PASSWORD;
PASSWORD populateUppercase(void);
PASSWORD populateLowercase(void);
PASSWORD populateNumbers(void);
PASSWORD populateSymbols(void);
#endif
* Include guards and #pragma once prevent a header file from being included multiple
times during compilation, which avoids errors like multiple definitions or
redefinitions of functions and variables. Without them, repeated inclusions
(especially through nested headers) can lead to compiler errors.
- the pragma once is not officially part of the C standard hence the include
guards are most often used
CHARSETS.C
/*######################################################################################
# Dev: cnd.dev
# Program Name: N/A
# Version: 1.0.0
# - Major.Minor.Update/BuildNumber
# Date: 231504JUN25
# Filename: charsets.c
# Dependency: charsets.h
# Compile Cmd: N/A
# Synopsis:
# - Overview: Used as part of utilities needing predefined character sets such as
# password generators, encoders, or validation tools.
# - Technical: Implements character array population functions declared in charsets.h.
# Each function fills an array with a specific category of ASCII characters.
######################################################################################*/
#include <stddef.h>
#include "charsets.h"
PASSWORD populateUppercase(void){
PASSWORD uppercase = {'\0'};
size_t arraySize = sizeof(uppercase.uppercaseLetters) / sizeof(uppercase.uppercaseLetters[0]);
for (int i = 0; i < arraySize - 1; i++){
uppercase.uppercaseLetters[i] = i + 'A'; // 'A' = 65
}
uppercase.uppercaseLetters[arraySize - 1] = '\0';
return uppercase;
}
PASSWORD populateLowercase(void){
PASSWORD lowercase = {'\0'};
size_t arraySize = sizeof(lowercase.lowercaseLetters) / sizeof(lowercase.lowercaseLetters[0]);
for (int i = 0; i < arraySize - 1; i++){
lowercase.lowercaseLetters[i] = i + 'a'; // 'a' = 97
}
lowercase.lowercaseLetters[arraySize - 1] = '\0';
return lowercase;
}
PASSWORD populateNumbers(void){
PASSWORD number = {'\0'};
size_t arraySize = sizeof(number.numbers) / sizeof(number.numbers[0]);
for (int i = 0; i < arraySize - 1; i++){
number.numbers[i] = i + '0';
}
number.numbers[arraySize - 1] = '\0';
return number;
}
PASSWORD populateSymbols(void){
PASSWORD special = {'\0'};
size_t arraySize = sizeof(special.symbols) / sizeof(special.symbols[0]);
/*** Total Symbols = 32 ***/
//ASCII 33–47: ! " # $ % & ' ( ) * + , - . /
for (int i = 0; i < 15; i++){
special.symbols[i] = i + 33;
}
//ASCII 58–64: : ; < = > ? @
for (int i = 0; i < 7; i++){
special.symbols[i + 15] = i + 58;
}
//ASCII 91–96: [ \ ] ^ _ `
for (int i = 0; i < 6; i++){
special.symbols[i + 22] = i + 91;
}
//ASCII 123–126: { | } ~
for (int i = 0; i < 4; i++){
special.symbols[i + 28] = i + 123;
}
return special;
}
GENERATOR.H
/*######################################################################################
# Dev: cnd.dev
# Program Name: N/A
# Version: 1.0.0
# - Major.Minor.Update/BuildNumber
# Date: 231504JUN25
# Filename: charsets.h
# Dependency: N/A
# Compile Cmd: N/A
# Synopsis:
# - Overview: Declares functions for populating arrays with specific character sets
# (uppercase, lowercase, digits, symbols) using ASCII values.
# - Technical: Intended for programs that need predefined character arrays, such as
# password generators or data format validators.
######################################################################################*/
#ifndef GENERATOR_H
#define GENERATOR_H
#include "charsets.h"
PASSWORD passwordGenerator(int passwordLength);
#endif
* Include guards and #pragma once prevent a header file from being included multiple
times during compilation, which avoids errors like multiple definitions or
redefinitions of functions and variables. Without them, repeated inclusions
(especially through nested headers) can lead to compiler errors.
- the pragma once is not officially part of the C standard hence the include
guards are most often used
GENERATOR.C
/*######################################################################################
# Dev: cnd.dev
# Program Name: N/A
# Version: 1.0.0
# - Major.Minor.Update/BuildNumber
# Date: 231504JUN25
# Filename: charsets.h
# Dependency: N/A
# Compile Cmd: N/A
# Synopsis:
# - Overview: Declares functions for populating arrays with specific character sets
# (uppercase, lowercase, digits, symbols) using ASCII values.
# - Technical: Intended for programs that need predefined character arrays, such as
# password generators or data format validators.
######################################################################################*/
#include <stddef.h>
#include <stdlib.h>
#include "generator.h"
#include "string.h"
PASSWORD passwordGenerator(int passwordLength){
char generatedPassword[passwordLength];
PASSWORD password = {'\0'};
size_t arraySize = sizeof(password.randomPassword) / sizeof(password.randomPassword[0]);
//fill
//aggregate variable hold all {uppercase} + {lowercase} + {numbers} + {symbols}
PASSWORD uppercaseCharacters = populateUppercase();
PASSWORD lowercaseCharacters = populateLowercase();
PASSWORD digitsCharacters = populateNumbers();
PASSWORD specialCharacters = populateSymbols();
strcat(password.collection, uppercaseCharacters.uppercaseLetters);
strcat(password.collection, lowercaseCharacters.lowercaseLetters);
strcat(password.collection, digitsCharacters.numbers);
strcat(password.collection, specialCharacters.symbols);
//Knuth Shuffle Algorithm - use user supplied password length here to only get that specified number of password
//shuffle the collection then assign user specified passwordLength to retrive from collection
size_t collectionLength = strlen(password.collection);
for (int i = collectionLength - 1; i > 0; i--){
int j = rand() % (i + 1);
char temp = password.collection[i];
password.collection[i] = password.collection[j];
password.collection[j] = temp;
}
//copy user supplied passwordLength characters to randomPassword
strncpy(password.randomPassword, password.collection, passwordLength);
password.randomPassword[passwordLength] = '\0';
return password;
}
PASSWORDGENERATOR.C
/*######################################################################################
# Dev: cnd.dev
# Program Name: PasswordGenerator-v1.0.0-linux-x86-64
# Version: 1.0.0
# - Major.Minor.Update/BuildNumber
# Date: 230900JUN25
# Filename: randomPWGenerator.c
# Dependency: N/A
# Compile Cmd: gcc -m64 -O1 passwordGenerator.c charsets.c generator.c -o PasswordGenerator-v1.0.0-linux-x86-64
# Synopsis:
# - Overview: describes what the program does, how it works, and its key components
# - Technical: ...
######################################################################################*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "charsets.h"
#include "generator.h"
int main()
{
srand(time(NULL));
PASSWORD password = {'\0'};
int passwordLength = 0;
//INPUT
puts("Password Generator v1.0");
printf("Enter Password Length: ");
scanf("%32d", &passwordLength);
//PROCESS
password = passwordGenerator(passwordLength);
//OUTPUT
printf("Password: %s", password.randomPassword);
return 0;
}
* time(NULL) returns the number of seconds since the Unix epoch (Jan 1, 1970).
* srand(time(NULL)) ensures rand() produces a different sequence each run.
FUTURE IMPROVEMENTS
Last updated