summaryrefslogtreecommitdiff
path: root/deonebook.c
blob: 12a67a5bdd526ac7b91061828584dd8dab5da2df (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
#define _XOPEN_SOURCE 700
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "deonebook.h"

int main(int argc, char *argv[])
{
	char *keystring = NULL;
	char *device = NULL;

	int c;
	while ((c = getopt(argc, argv, "k:d:")) != -1) {
		switch (c) {
		case 'k':
			keystring = optarg;
			break;

		case 'd':
			device = optarg;
			break;

		default:
			return 1;
		}
	}

	unsigned char *key = getkey(keystring, device);

	if (key == NULL) {
		return 1;
	}

	if (optind >= argc) {
		for (int i = 0; i < KEY_SIZE; i++) {
			printf("%02hhx", key[i]);
		}
		printf("\n");
		return 0;
	}

	if (optind < argc - 2) {
		fprintf(stderr, "too many operands\n");
		return 1;
	}

	if (optind != argc - 2) {
		fprintf(stderr, "missing output file\n");
		return 1;
	}

	return decrypt(key, argv[optind], argv[optind + 1]);
}