Skip to content

Commit 07fa6ca

Browse files
committed
Adds -K (capital K) option
* 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.
1 parent a892003 commit 07fa6ca

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Release version: 2.1.0
2+
* **Feature:** -K (capital k), reads key from the stdin. This way your key will
3+
not get saved to the terminal history. The old option is still present and
4+
is backwards compatable with old .3 files.
5+
16
## Release version: 2.0.1
27
* All header files are inside the headers folder.
38
* Binary for linux is built inside the bin/linux folder.

main.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"-N - When decrypting, display output to stdout.\n" \
3535
"-D - Deletes source files after encryption or decryption.\n" \
3636
"-v - Verbose\n" \
37-
"-k - 16 byte key.\n" \
37+
"-k - 16 byte key (as argument).\n" \
38+
"-K - 16 byte key (from stdin).\n" \
3839
"-I - Files that need to be processed.\n" \
3940
"\nNotes:\n"\
4041
" - Cannot use -D (Delete file), -N (stdout output) together.\n" \
@@ -67,7 +68,7 @@ struct op
6768
int readargs(char *argv[], struct op *out);
6869
bool delete(char *filename);
6970
char **read_args_files(char *argv[], struct op *out);
70-
int read_args_key(char *arg, struct op *out);
71+
int read_args_key(char *arg, struct op *out, bool fromstdin);
7172
int strip_extension(char *filename, char *extension, char *out);
7273
bool args_is_valid(struct op *out);
7374

@@ -182,6 +183,8 @@ int strip_extension(char *filename, char *extension, char *out)
182183
/*
183184
* It will read the startup arguments and fill the 'op' structure.
184185
* */
186+
#define KEY_FROM_ARGS false
187+
#define KEY_FROM_STDIN true
185188
int readargs(char *argv[], struct op *out)
186189
{
187190
char *arg;
@@ -201,7 +204,10 @@ int readargs(char *argv[], struct op *out)
201204
out->mode = DECRYPT;
202205
break;
203206
case 'k':
204-
read_args_key(*argv++,out);
207+
read_args_key(*argv++,out,KEY_FROM_ARGS);
208+
break;
209+
case 'K':
210+
read_args_key(NULL,out,KEY_FROM_STDIN);
205211
break;
206212
case 'I':
207213
argv = read_args_files(argv,out);
@@ -256,15 +262,31 @@ bool args_is_valid(struct op *out)
256262
return true;
257263
}
258264

259-
int read_args_key(char *arg, struct op *out)
265+
int read_args_key(char *arg, struct op *out, bool fromstdin)
260266
{
261-
if (strlen(arg) != KEY_SIZE){
267+
char *key = arg;
268+
269+
if (fromstdin){
270+
/* Ask for the key and read from stdin */
271+
272+
// +1 because length argument must include the EOL character.
273+
char inkey[KEY_SIZE + 1];
274+
key = inkey;
275+
276+
printf("Enter key (%d characters): ", KEY_SIZE);
277+
if (fgets(key, sizeof(inkey), stdin) == NULL){
278+
perror("fgets");
279+
return ERR_INVALID_ARG;
280+
}
281+
}
282+
283+
if (strlen(key) != KEY_SIZE){
262284
fprintf(stderr,
263285
"Error: Invalid key. Must be %u bytes long.\n", KEY_SIZE);
264286
return ERR_INVALID_ARG;
265287
}
266288

267-
memcpy(out->key,arg,KEY_SIZE);
289+
memcpy(out->key,key,KEY_SIZE);
268290
return 0;
269291
}
270292

0 commit comments

Comments
 (0)