ecrypt 2.0.0

This commit is contained in:
Liam Waldron 2023-05-16 11:54:19 -04:00
parent 8dba658d6c
commit dc00b66cf9
7 changed files with 213 additions and 4 deletions

View File

@ -1,9 +1,12 @@
include config.mk
all: src/main.c
$(CC) $(CFLAGS) $(LDFLAGS) src/main.c -o src/ecrypt
all:
$(CC) $(CFLAGS) $(LDFLAGS) -c src/ecrypt.c -o src/ecrypt.o
$(CC) $(CFLAGS) $(LDFLAGS) -c src/checkkey.c -o src/checkkey.o
$(CC) $(CFLAGS) $(LDFLAGS) -c src/genkey.c -o src/genkey.c
$(CC) $(CFLAGS) $(LDFLAGS) -o src/ecrypt src/ecrypt.o src/checkkey.o src/genkey.o
install: src/ecrypt man/ecrypt.1
install:
install src/ecrypt $(PREFIX)/bin
install -g 0 -o 0 -m 0644 man/ecrypt.1 /usr/local/man/man1
install -g 0 -o 0 -m 0644 man/ecrypt.1 $(PREFIX)/share/man/man1
gzip /usr/local/man/man1/ecrypt.1

9
ecrypt.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef ECRYPT_H_
#define ECRYPT_H_
#define ECRYPT_VERSION "2.0.0"
void genkey(int N);
int checkkey(int argc, char *argv[]);
#endif

70
src/checkkey.c Normal file
View File

@ -0,0 +1,70 @@
#include <color.h>
#include <stdio.h>
#include <string.h>
int
checkkey(int argc, char *argv[])
{
FILE *key1;
FILE *key2;
int cnt1 = 0;
int cnt2 = 0;
int flg = 0;
if (argv < 3) {
printf("usage: %s [-g] [-c] [-h] [-v] KEYFILE KEY\n", argv[0]);
return 1;
}
key1 = fopen(argv[1], "r");
if (key1 == NULL) {
printf("cannot open local first key file\n");
return 1;
}
fseek(key1, 0, SEEK_END);
cnt1 = ftell(key1);
key2 = fopen(argv[2], "r");
if (key2 == NULL) {
printf("cannot open second key file\n");
return 1;
}
fseek(key2, 0, SEEK_END);
cnt2 = ftell(key2);
fseek(key1, 0, SEEK_SET);
fseek(key2, 0, SEEK_SET);
if (cnt1 != cnt2) {
printf(COL_RED "keys do not match\n" COL_RESET);
return 1;
} else {
while (! feof(key1)) {
if (fgetc(key1) != fgetc(key2)) {
flg = 1;
break;
}
}
if (flg) {
printf(COL_RED "keys do not match\n" COL_RESET);
return 1;
} else {
printf(COL_GREEN "keys match\n" COL_RESET);
return 0;
}
}
fclose(key1);
fclose(key2);
return 0;
}
int
main(int argc, char *argv[])
{
checkkey(argc, argv);
}

79
src/ecrypt.c Normal file
View File

@ -0,0 +1,79 @@
#include <color.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ecrypt.h"
#include "config.h"
static void
usage(int argc, char *argv[])
{
printf("%s - key generator and checker\n", argv[0]);
printf("usage: %s [-h] [-v] [-s] [-g] [-c]\n", argv[0]);
printf("\n");
printf("%s {-h} Show this message\n", argv[0]);
printf("%s {-v} Show the current version\n", argv[0]);
printf("%s {-s} View compile-time settings\n", argv[0]);
printf("%s {-g} Generate a key\n", argv[0]);
printf("%s {-c} Compare 2 key files\n", argv[0]);
printf("\n");
printf("Ecrypt is free software.\n");
printf("See the GNU GPL version 3 for details.\n");
}
static void
usage_small(int argc, char *argv[])
{
printf("usage: %s [-h] [-v] [-s] [-g] [-c]\n", argv[0]);
}
static void
view_compile_time_settings(int argc, char *argv[])
{
printf("Compile-time settings for %s\n", argv[0]);
printf("Key length: " PASSWD_LENGTH "\n");
}
int
main(int argc, char *argv[])
{
if (argc < 2) {
usage_small(argc, argv);
exit(1);
}
int opt;
while ((opt = getopt(argc, argv, ":if:hvsgc")) != -1) {
switch(opt) {
case 'h':
usage(argc, argv);
exit(0);
break;
case 'v':
printf(ECRYPT_VERSION "\n");
exit(0);
break;
case 's':
view_compile_time_settings(argc, argv);
exit(0);
break;
case 'g':
genkey(N);
exit(0);
break;
case 'c':
checkkey(argc, argv);
exit(0);
break;
case '?':
usage_small(argc, argv);
exit(1);
break;
}
}
return 0;
}

48
src/genkey.c Normal file
View File

@ -0,0 +1,48 @@
#include <color.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "config.h"
void
genkey(int N)
{
int count = 0;
int randomizer = 0;
srand((unsigned int)(time(NULL)));
char numbers[] = "1234567890";
char letter[] = "abcdefghijklmnopqrstuvwxyz";
char LETTER[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char symbols[] = "!@#$%^&*?";
char key[N];
randomizer = rand() % 4;
for (count = 0; count < N; count++) {
switch (randomizer) {
case 1:
key[count] = numbers[rand() % 10];
randomizer = rand() % 4;
printf(COL_BLUE "%c" COL_RESET, key[count]);
break;
case 2:
key[count] = symbols[rand() % 8];
randomizer = rand() % 4;
printf(COL_BLUE "%c" COL_RESET, key[count]);
break;
case 3:
key[count] = LETTER[rand() % 26];
randomizer = rand() % 4;
printf(COL_BLUE "%c" COL_RESET, key[count]);
break;
default:
key[count] = letter[rand() % 26];
randomizer = rand() % 4;
printf(COL_BLUE "%c" COL_RESET, key[count]);
}
}
}