summaryrefslogtreecommitdiffstats
path: root/old_code/keygen.c
blob: b03b89cc3c0badce2fa35e77b3f001f25536b60c (plain)
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
#include<stdio.h>
#include<openssl/rand.h>
#include<unistd.h>

int main(int argc, char* argv[]) {

	int opt;
	int nbytes = 256;
	char* defname = "default.key";
	char* output;
	int defout = 0;
	int defbyte = 0;
	int noopts = 0;
	while ((opt = getopt(argc, argv, "b:o:")) != -1) {
		switch (opt) {
		case 'b':
			nbytes = atoi(optarg);
			defbyte = 1;
			break;
		case 'o':
			output = argv[(optind-1)];
			defout = 1;
			break;
		default:
			fprintf(stderr, "Usage: %s [-b nbytes] [-o output] \n", argv[0]);
			exit(EXIT_FAILURE);
		}
		if (noopts == 0) {
			noopts = 1;
		}
	}

	if(noopts == 0) {
		printf("WARNING no option specified usign default values... \n");
	}
	if (nbytes < 0) {
		printf("Negative byte value inserted! \n");
		printf("Exiting... \n");
		exit(EXIT_FAILURE);
	} else if (nbytes != 0) {
		if (defbyte == 0) {
			printf("No byte number specified... using default value: 256 \n");
		}
		unsigned char key[nbytes];
		RAND_bytes(key, nbytes);
		if (defout == 0) {
			output = defname;
			printf("No output name specified... using default value: default.key \n");
		}
		FILE* file = fopen(output, "w");
		fwrite(key, nbytes, 1, file);
		fclose(file);
		printf("Created key file %s of %d bytes \n", output, nbytes);
	} else {
		printf("Byte number specified is 0. \n");
		printf("Doing nothing! \n");
	}

	exit(EXIT_SUCCESS);
}