blob: fd509d2645ad20d12a0e1a71159ccc0bd46a92b6 (
plain) (
blame)
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
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#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()
{
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 (for now)
srand(time(NULL) + 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; \
break;
switch (c) {
case 'c':
setClass(CONSONANTS);
case 'v':
setClass(VOWELS);
case '!':
setClass(SYMBOLS);
case '#':
setClass(NUMBERS);
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;
}
|