-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeygen.c
75 lines (66 loc) · 1.69 KB
/
keygen.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
* A key generator used for one-time-pad encryption.
* allows user to generate key of specified length.
*/
// Constants
const int NUM_CHARS = 27; // A-Z + Space
const int ASCII_ALPHA_START = 65;
const int ASCII_ALPHA_END = 90;
const int ASCII_SPACE = 32;
// Use time-based random number generation to avoid same numbers each run
void generate_new_seed(void)
{
time_t t;
srand((unsigned) time(&t));
}
// Get a random alpha character, includes the space character
char get_random_char(void)
{
char random_char = ASCII_ALPHA_START;
random_char += rand() % NUM_CHARS;
if (random_char > ASCII_ALPHA_END)
random_char = ASCII_SPACE;
return random_char;
}
// Create a key using random characters
char* generate_key(int length)
{
char* key = calloc(length + 1, sizeof(char));
for (int i = 0; i < length; i++)
{
key[i] = get_random_char();
}
return key;
}
// Key generator, first argument is the length of the key to generate
int main(int argc, char *argv[]) {
int length = 0;
char* key = NULL;
// Check for supplied length argument
if (argc > 1)
{
// Convert length arg from string to integer
length = atoi(argv[1]);
// Check if length is a valid, non-zero integer
if (length <= 0)
{
fprintf(stderr, "ERROR: Invalid Key Length\n");
}
else
{
// Key Generated to stdout
generate_new_seed();
key = generate_key(length);
printf("%s\n", key);
free(key);
}
}
else
{
fprintf(stderr, "ERROR: Missing Key Length\n");
}
return 0;
}