Skip to content

Commit

Permalink
Adds -K (capital K) option
Browse files Browse the repository at this point in the history
* When -K option is used, key is taken from stdin and not from
  arguments. This prevents the secret key to be saved to the
  terminal history file along with the command.
  • Loading branch information
coderarjob committed Dec 2, 2021
1 parent a892003 commit 07fa6ca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Release version: 2.1.0
* **Feature:** -K (capital k), reads key from the stdin. This way your key will
not get saved to the terminal history. The old option is still present and
is backwards compatable with old .3 files.

## Release version: 2.0.1
* All header files are inside the headers folder.
* Binary for linux is built inside the bin/linux folder.
Expand Down
34 changes: 28 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"-N - When decrypting, display output to stdout.\n" \
"-D - Deletes source files after encryption or decryption.\n" \
"-v - Verbose\n" \
"-k - 16 byte key.\n" \
"-k - 16 byte key (as argument).\n" \
"-K - 16 byte key (from stdin).\n" \
"-I - Files that need to be processed.\n" \
"\nNotes:\n"\
" - Cannot use -D (Delete file), -N (stdout output) together.\n" \
Expand Down Expand Up @@ -67,7 +68,7 @@ struct op
int readargs(char *argv[], struct op *out);
bool delete(char *filename);
char **read_args_files(char *argv[], struct op *out);
int read_args_key(char *arg, struct op *out);
int read_args_key(char *arg, struct op *out, bool fromstdin);
int strip_extension(char *filename, char *extension, char *out);
bool args_is_valid(struct op *out);

Expand Down Expand Up @@ -182,6 +183,8 @@ int strip_extension(char *filename, char *extension, char *out)
/*
* It will read the startup arguments and fill the 'op' structure.
* */
#define KEY_FROM_ARGS false
#define KEY_FROM_STDIN true
int readargs(char *argv[], struct op *out)
{
char *arg;
Expand All @@ -201,7 +204,10 @@ int readargs(char *argv[], struct op *out)
out->mode = DECRYPT;
break;
case 'k':
read_args_key(*argv++,out);
read_args_key(*argv++,out,KEY_FROM_ARGS);
break;
case 'K':
read_args_key(NULL,out,KEY_FROM_STDIN);
break;
case 'I':
argv = read_args_files(argv,out);
Expand Down Expand Up @@ -256,15 +262,31 @@ bool args_is_valid(struct op *out)
return true;
}

int read_args_key(char *arg, struct op *out)
int read_args_key(char *arg, struct op *out, bool fromstdin)
{
if (strlen(arg) != KEY_SIZE){
char *key = arg;

if (fromstdin){
/* Ask for the key and read from stdin */

// +1 because length argument must include the EOL character.
char inkey[KEY_SIZE + 1];
key = inkey;

printf("Enter key (%d characters): ", KEY_SIZE);
if (fgets(key, sizeof(inkey), stdin) == NULL){
perror("fgets");
return ERR_INVALID_ARG;
}
}

if (strlen(key) != KEY_SIZE){
fprintf(stderr,
"Error: Invalid key. Must be %u bytes long.\n", KEY_SIZE);
return ERR_INVALID_ARG;
}

memcpy(out->key,arg,KEY_SIZE);
memcpy(out->key,key,KEY_SIZE);
return 0;
}

Expand Down

0 comments on commit 07fa6ca

Please sign in to comment.