From 073cdd24686763ebd7cb686a86eff946ac2ca1e7 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Thu, 26 May 2022 18:35:15 +0200 Subject: [PATCH] moved checking for valid key to misc I'm trying to make code variable as much as possible --- include/misc.h | 1 + src/lib/decrypter.c | 6 +----- src/lib/encrypter.c | 6 +----- src/lib/misc.c | 9 +++++++++ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/misc.h b/include/misc.h index 03f6cfb..187d121 100644 --- a/include/misc.h +++ b/include/misc.h @@ -6,6 +6,7 @@ void checkVersion(inputFlags flags); //THIS FUNCTION CHECKS IF LATEST VERSION OF WHY2 IS USED void generateTextKeyChain(char key[], int *textKeyChain, int textKeyChainSize); //GENERATES ARRAY FOR ENCRYPTION/DECRYPTION void deallocateOutput(outputFlags flags); //DEALLOCATES flags +void checkKey(char *key, inputFlags flags); //CHECKS IF KEY IS VALID int countIntLength(int number); //RETURNS LENGTH OF number #endif diff --git a/src/lib/decrypter.c b/src/lib/decrypter.c index 0611f41..3f8368e 100644 --- a/src/lib/decrypter.c +++ b/src/lib/decrypter.c @@ -10,11 +10,7 @@ outputFlags decryptText(char *text, char *key, inputFlags flags) { //CHECK FOR INVALID key - if (strlen(key) < getKeyLength()) - { - if (!flags.noOutput) fprintf(stderr, "Key must be at least %d characters long!\n", getKeyLength()); - exit(INVALID_KEY); - } + checkKey(key, flags); //REDEFINE keyLength setKeyLength(strlen(key)); diff --git a/src/lib/encrypter.c b/src/lib/encrypter.c index 5cb084d..be5af56 100644 --- a/src/lib/encrypter.c +++ b/src/lib/encrypter.c @@ -29,11 +29,7 @@ outputFlags encryptText(char *text, char *keyNew, inputFlags flags) if (keyNew != NULL) { - if (strlen(keyNew) < getKeyLength()) - { - if (!flags.noOutput) fprintf(stderr, "Key must be at least %d characters long!\n", getKeyLength()); - exit(INVALID_KEY); - } + checkKey(key, flags); //CHECK FOR INVALID key strcpy(key, keyNew); diff --git a/src/lib/misc.c b/src/lib/misc.c index 2b5dbe2..3bd8153 100644 --- a/src/lib/misc.c +++ b/src/lib/misc.c @@ -168,6 +168,15 @@ void deallocateOutput(outputFlags flags) free(flags.usedKey); } +void checkKey(char *key, inputFlags flags) +{ + if (strlen(key) < getKeyLength()) + { + if (!flags.noOutput) fprintf(stderr, "Key must be at least %d characters long!\n", getKeyLength()); + exit(INVALID_KEY); + } +} + int countIntLength(int number) { int returning = 1;