163 lines
3.4 KiB
C
163 lines
3.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <string.h>
|
|
|
|
#define error "minimal_length maximal_length options custom_characters\n"
|
|
|
|
int str_to_int(char* str) {
|
|
int output = 0;
|
|
|
|
for (int i = 0; i < strlen(str); i++) {
|
|
if (str[i] < '0' || str[i] > '9') {
|
|
printf("That is not a number.\n");
|
|
exit(1);
|
|
}
|
|
|
|
output *= 10;
|
|
output += str[i] - '0';
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
void not_avaliable_options(char** argv) {
|
|
if (strcmp(argv[3], "-c") != 0) {
|
|
printf(error);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
void invalid_args(int argc) {
|
|
if (argc < 4 && argc > 5) {
|
|
printf("nuhuh\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
char* character_list(int argc, char** argv) {
|
|
const char uppercase[] = "QWERTZUIOPASDFGHJKLYXCVBNM";
|
|
const char lowercase[] = "qwertzuiopasdfghjklyxcvbnm";
|
|
const char symbols[] = "/!?._-#&";
|
|
const char numbers[] = "0123456789";
|
|
|
|
char* output;
|
|
|
|
if (argc == 4) {
|
|
int final_size = 0;
|
|
|
|
for (int i = 0; argv[3][i] != '\0'; i++) {
|
|
switch (argv[3][i]) {
|
|
case 'U':
|
|
final_size += sizeof(uppercase) - 1;
|
|
break;
|
|
case 'l':
|
|
final_size += sizeof(lowercase) - 1;
|
|
break;
|
|
case 's':
|
|
final_size += sizeof(symbols) - 1;
|
|
break;
|
|
case 'n':
|
|
final_size += sizeof(numbers) - 1;
|
|
break;
|
|
default:
|
|
printf(error);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
output = malloc(sizeof(char) * (final_size + 1));
|
|
|
|
int last_index = 0;
|
|
const char* current_list;
|
|
int size;
|
|
|
|
for (int i = 0; argv[3][i] != '\0'; i++) {
|
|
switch (argv[3][i]) {
|
|
case 'U':
|
|
current_list = uppercase;
|
|
size = sizeof(uppercase) - 1;
|
|
break;
|
|
case 'l':
|
|
current_list = lowercase;
|
|
size = sizeof(lowercase) - 1;
|
|
break;
|
|
case 's':
|
|
current_list = symbols;
|
|
size = sizeof(symbols) - 1;
|
|
break;
|
|
case 'n':
|
|
current_list = numbers;
|
|
size = sizeof(numbers) - 1;
|
|
break;
|
|
}
|
|
|
|
for (int j = 0; j < size; j++) {
|
|
output[last_index] = current_list[j];
|
|
last_index++;
|
|
}
|
|
}
|
|
output[final_size] = '\0';
|
|
}
|
|
|
|
else if (argc == 5) {
|
|
int final_size = 0;
|
|
|
|
for (int i = 0; argv[4][i] != '\0'; i++) {
|
|
final_size++;
|
|
}
|
|
|
|
output = malloc(sizeof(char) * (final_size + 1));
|
|
|
|
for (int i = 0; argv[4][i] != '\0'; i++) {
|
|
output[i] = argv[4][i];
|
|
}
|
|
|
|
output[final_size] = '\0';
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
int random_len(int min_length, int max_length) {
|
|
return rand() % (max_length - min_length + 1) + min_length;
|
|
}
|
|
|
|
char* random_str(int length, char* symbols) {
|
|
char* str = malloc(sizeof(char) * (length + 1));
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
int index = rand() % strlen(symbols);
|
|
str[i] = symbols[index];
|
|
}
|
|
|
|
str[length] = '\0';
|
|
|
|
return str;
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
invalid_args(argc);
|
|
|
|
if (argc == 5) {
|
|
not_avaliable_options(argv);
|
|
}
|
|
|
|
srand(time(NULL));
|
|
|
|
int min_length = str_to_int(argv[1]);
|
|
int max_length = str_to_int(argv[2]);
|
|
|
|
if (min_length > max_length) {
|
|
printf("Your minimal length is longer than your maximal length.\n");
|
|
exit(1);
|
|
}
|
|
|
|
int length = random_len(min_length, max_length);
|
|
|
|
char* symbols = character_list(argc, argv);
|
|
char* password = random_str(length, symbols);
|
|
free(symbols);
|
|
puts(password);
|
|
free(password);
|
|
}
|