-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.c
More file actions
349 lines (319 loc) · 9.46 KB
/
Copy pathgen.c
File metadata and controls
349 lines (319 loc) · 9.46 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Passphrase Generator: Command line tool to generate cryptographically secure passphrases
* Copyright (C) 2025, Ada Gramiak, <adadispenser@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include "lib/map.h"
unsigned int length = 16;
char* seperator = " ";
unsigned char words = 5;
struct CharMap number_map;
// mutators will have a 1 in x chance of applying per character
unsigned int mutation_chance = 4;
unsigned char length_mode = 0;
unsigned char capitalize = 0;
unsigned char numbers = 0;
unsigned char misspell = 0;
unsigned char special_chars = 0;
int is_alpha(char input) {
if (input >= 'A' && input <= 'Z') {
return 1;
}
if (input >= 'a' && input <= 'z') {
return 1;
}
return 0;
}
char lowercase(char input) {
// convert alphabetic character to lowercase
if (!is_alpha(input)) {
return 0;
}
if (input >= 'a' && input <= 'z') {
return input;
}
return (input - 'A' + 'a');
}
unsigned short rng() {
// read 2 bytes from /dev/random
unsigned short output;
FILE* random = fopen("/dev/random", "rb");
fread(&output, sizeof(short), 1, random);
fclose(random);
return output;
}
char* get_word() {
// get a random word from alpha.txt
// init variables
unsigned short index = rng();
char c = 0;
unsigned short line_num = 0;
unsigned short line_index = 0;
size_t line_len = 1;
char* line = malloc(line_len * sizeof(char));
// open words.txt
FILE* words = NULL;
words = fopen("./wordlists/alpha.txt", "r");
if (words == NULL) {
puts("ERROR: Unable to open wordlist!");
puts("Please ensure a file named words.txt exists in the same directory as the executable.");
exit(-1);
}
// read each line of words.txt into the line variable
while (c != EOF) {
c = fgetc(words);
if (c == '\n') {
// end of line
if (line_num == index) {
line[line_index] = 0; // add null terminator
fclose(words);
return line + (5 * sizeof(char)); // 5 characters ahead to cut out the tab and hex index
}
line_num++;
line_index = 0;
continue;
}
// add character to line
line[line_index] = c;
line_index++;
// resize line if necessary
if (line_index == line_len) {
line = realloc(line, ++line_len);
}
}
// this should never be called unless line.txt gets shortened
fclose(words);
printf("ERROR: could not find word on line %u!", index);
exit(-1);
}
int stoi(char* input) {
// convert numerical string to int
int i = 0;
char c;
int output = 0;
while (i <= strlen(input)) {
c = input[i];
if (c == 0) {
break;
}
if (c >= '0' && c <= '9') {
output = output * 10 + (c - '0'); // convert numerical character to integer, append to output
} else {
printf("Error: expected an integer argument, got \"%s\" instead.\n", input);
exit(-1);
}
i++;
}
return output;
}
void help() {
puts("passphrase: passphrase [-cnmp] [-l number] [-w number] [-s string]");
puts(" Generate a passphrase");
puts("");
puts(" Options:");
puts(" -h \t\t: Print this menu.");
puts(" -c \t\t: Randomy capitalize letters (example: anTimoNY)");
puts(" -n \t\t: Randomy replace letters with numbers (example: ant1m0ny)");
/* not fully implemented */puts(" -m \t\t: Randomy misspell words (example: animuny)");
/* not implemented */puts(" -p \t\t: Randomy add special characters (example: @ntim*ny)");
/* not implemented */puts(" -l [number]\t: Generate password of a specific length. If no length is specified, the default is 16 characters.");
puts(" -w <number>\t: Specify the number of words in passphrase. Default is 5.");
puts(" -u <number>\t: Specify mutator chance. (1 in x) Default is 4.");
puts(" -s <\"string\">\t: Specify the seperator to be used between words. If multiple characters are provided they will be chosen from randomly. Default is space. (0x20)");
puts("");
puts(" Exit Status:");
puts(" Returns success unless words.txt is unable to be found, or if bad arguments are given.");
}
char* gen_passphrase() {
// generate string of words, the output of this function can be used as a passphrase if no modifiers are selected
char* passphrase = malloc(sizeof(char));
char* word;
unsigned int seperator_count = strlen(seperator);
if (length_mode) {
// TODO: Write length mode generation
// generate a passphrase of a certain character count
passphrase = realloc(passphrase, length * sizeof(char)); // idk why i always put * sizeof(char) in mallocs it does nothing :skull:
word = get_word();
if (misspell) {
}
} else {
// generate a passphrase of a certain word count
int i = 0;
while (i < words) {
word = get_word();
// resize passphrase for concatenation and seperator character
passphrase = realloc(passphrase, (strlen(passphrase) + strlen(word) + 2) * sizeof(char));
passphrase = strcat(passphrase, word);
if (seperator_count <= 1) {
passphrase = strcat(passphrase, seperator);
} else {
int index = rng() % strlen(seperator);
char* temp = malloc(2 * sizeof(char));
temp[0] = seperator[index];
temp[1] = 0;
passphrase = strcat(passphrase, temp);
free(temp);
}
i++;
}
}
// remove trailing seperator
passphrase[strlen(passphrase) - 1] = 0;
return passphrase;
}
char* random_caps(char* input) {
// capitalize letters randomly based on globally specified mutation chance
int i = 0;
int string_length = strlen(input);
while (i < string_length) {
if (rng() % mutation_chance != 0) {
i++;
continue;
}
if (input[i] < 'a' || input[i] > 'z') {
i++;
continue;
}
input[i] = input[i] - 'a' + 'A';
i++;
}
return input;
}
char* random_numbers(char* input) {
// replace letters with numbers randomly based on globally specified mutation chance
int i = 0;
while (i < strlen(input)) {
char c = input[i];
if (rng() % mutation_chance != 0) {
i++;
continue;
}
if (!is_alpha(c)) {
i++;
continue;
}
c = lowercase(c);
char replaced;
if (misspell) {
if (rng() & 1) {
int rand;
rand = rng() & 0b1111;
while (rand > 9) {
rand = rng() & 0b1111;
}
replaced = '0' + rand;
printf("misspell replacing %c with %c\n", input[i], replaced);
input[i] = replaced;
i++;
continue;
}
}
int return_code = map_get(&number_map, c, &replaced);
if (return_code != 0) {
continue;
// printf("Warning! character %c not in number list!\n", c);
}
printf("replacing %c with %c\n", input[i], replaced);
input[i] = replaced;
i++;
}
return input;
}
int main(int argc, char **argv) {
int option;
char* temp_arg;
// parse arguments
while ((option = getopt(argc, argv, ":hcnmpl:w:u:s:")) != -1) {
switch (option) {
case 'h':
// print info and exit
help();
exit(0);
case 'c':
capitalize = 1;
break;
case 'n':
numbers = 1;
break;
case 'm':
misspell = 1;
break;
case 'p':
special_chars = 1;
break;
case 'l':
length_mode = 1;
length = stoi(optarg);
break;
case 'w':
words = stoi(optarg);
break;
case 's':
seperator = malloc((strlen(optarg) + 1) * sizeof(char));
strcpy(seperator, optarg);
break;
case 'u':
mutation_chance = stoi(optarg);
break;
case ':':
if (optopt == 'l') {
length_mode = 1;
break;
}
printf("Option \'%c\' missing value.\n", optopt);
exit(-1);
case '?':
printf("Unknown option \'%c\'\n", optopt);
exit(-1);
default:
printf("if you manage to get this error im genuinely impressed. (and hate getopt)\n");
exit(-1);
}
}
while (optind < argc) {
printf("extra arguments: %s\n", argv[optind]);
optind++;
}
// debug
/*
printf("Capitalize: %i\n", capitalize);
printf("Numbers: %i\n", numbers);
printf("Misspell: %i\n", misspell);
printf("Special Characters: %i\n", special_chars);
printf("Length Mode: %i\n", length_mode);
printf("Length: %i\n", length);
printf("Word Count: %i\n", words);
printf("Seperator Characters: \"%s\"\n", seperator);
*/
// initialize maps
number_map = full_map("abegiloqstz", "48361109572");
// generate passphrase
char* passphrase = gen_passphrase();
// apply mutators if applicable
if (capitalize) {
random_caps(passphrase);
}
if (numbers) {
random_numbers(passphrase);
}
// print passphrase to terminal
printf("%s\n", passphrase);
exit(0);
}