From c0986a09b7aa8eee49e207ecafb9ce88677dd84b Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Tue, 30 Nov 2021 21:44:20 +1100 Subject: Initial passgen with no customisable grammar --- passgen.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 passgen.c (limited to 'passgen.c') diff --git a/passgen.c b/passgen.c new file mode 100644 index 0000000..f4f8960 --- /dev/null +++ b/passgen.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include + +#define DEFAULT_GRAMMAR "Cvccvc!##" + +// i, o excluded due to potentially confusing 1/l/i + 0/o +#define VOWELS "aeuy" +#define CONSONANTS "bcdfghkmnprstvwxz" +#define NUMBERS "1234567890" +#define SYMBOLS "@#$%^&*_-+=()[]{}" + +int main(int argc, char *argv[]) +{ + char *grammar = DEFAULT_GRAMMAR; + int grammar_size = sizeof(DEFAULT_GRAMMAR)-1; + + char password[grammar_size]; + + // seed RNG; this isn't very good, but it's enough + srand(time(NULL) << 2 + getpid() % 420 - 69); + + for (int i = 0; i < grammar_size; ++i) { + char c = grammar[i]; + + bool caps = false; + if (c >= 'A' && c <= 'Z') { + caps = true; + c += 'a' - 'A'; + } + + char *class; + int class_size = 0; +#define setClass(cl) \ + class = cl; \ + class_size = sizeof(cl)-1; + + switch (c) { + case 'c': + setClass(CONSONANTS); + break; + case 'v': + setClass(VOWELS); + break; + case '!': + setClass(SYMBOLS); + break; + case '#': + setClass(NUMBERS); + break; + default: + printf("ERROR: Invalid grammar character '%C'.\n", c); + return 1; + } + + do { + password[i] = class[rand() % class_size] - (caps ? 'a' - 'A' : 0); + } while (i != 0 && password[i] == password[i-1]); + } + + printf("%s\n", password); + + return 0; +} -- cgit