blob: f4f89600259af81d20b0e9cde29d7a65b474f1b7 (
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
64
65
66
|
#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(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;
}
|