[c] Disabled shell echo when typing passphrase

This commit is contained in:
Afonso Franco 2024-02-23 10:50:20 +00:00
parent ca19746259
commit 7ea5e9c6e0
Signed by: afonso
SSH key fingerprint: SHA256:JiuxZNdA5bRWXPMUJChI0AQ75yC+cXY4xM0IaVwEVys

View file

@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#define KEY_SIZE 32
#define SALT_SIZE 16
@ -189,6 +190,21 @@ int decrypt(char *input_file, const char *passphrase) {
return 0;
}
void disableEcho() {
struct termios oldTermios, newTermios;
tcgetattr(0, &oldTermios);
newTermios = oldTermios;
newTermios.c_lflag &= ~(ECHO);
tcsetattr(0, TCSANOW, &newTermios);
}
void enableEcho() {
struct termios oldTermios;
tcgetattr(0, &oldTermios);
oldTermios.c_lflag |= ECHO;
tcsetattr(0, TCSANOW, &oldTermios);
}
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "Usage: %s {enc|dec} [file_path]\n", argv[0]);
@ -210,10 +226,12 @@ int main(int argc, char *argv[]) {
char passphrase[256]; // Assuming maximum passphrase length of 255 characters
printf("Enter passphrase: ");
disableEcho();
if (fgets(passphrase, sizeof(passphrase), stdin) == NULL) {
fprintf(stderr, "Error reading passphrase from stdin\n");
return 1;
}
enableEcho();
passphrase[strcspn(passphrase, "\n")] = '\0'; // Remove trailing newline
int suc = 0;
if (strcmp(mode, "enc") == 0) {