Tidied up code, removed obsolete getpass and implemented replacement
This commit is contained in:
parent
072db09093
commit
a0c81e7064
26
Makefile
26
Makefile
@ -1,12 +1,22 @@
|
||||
salty: salty.o
|
||||
cc ./build/salty.o -o ./build/salty -lsodium -I.
|
||||
rm ./build/salty.o
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -std=c99 -O2
|
||||
LDFLAGS = -lsodium
|
||||
BUILD_DIR = build
|
||||
PREFIX = /usr/local
|
||||
|
||||
salty.o: build
|
||||
cc -c salty.c -o ./build/salty.o -lsodium -I.
|
||||
all: $(BUILD_DIR)/salty
|
||||
|
||||
build:
|
||||
mkdir -p ./build
|
||||
$(BUILD_DIR)/salty: salty.c pawstd.h | $(BUILD_DIR)
|
||||
$(CC) $(CFLAGS) -o $@ salty.c $(LDFLAGS)
|
||||
|
||||
$(BUILD_DIR):
|
||||
mkdir -p $(BUILD_DIR)
|
||||
|
||||
clean:
|
||||
rm -rf $(BUILD_DIR)
|
||||
|
||||
install:
|
||||
install -m 755 ./build/salty /usr/local/bin/salty
|
||||
install -m 755 $(BUILD_DIR)/salty /usr/local/bin/salty
|
||||
|
||||
uninstall:
|
||||
rm $(PREFIX)/bin/salty
|
||||
63
salty.c
63
salty.c
@ -2,12 +2,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sodium.h>
|
||||
#include <pawstd.h>
|
||||
#include "pawstd.h"
|
||||
#define KEY_SIZE crypto_secretbox_KEYBYTES
|
||||
#define SALT_SIZE crypto_pwhash_SALTBYTES
|
||||
#define BUF_SIZE 1024
|
||||
@ -69,8 +66,6 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
int i = 0;
|
||||
|
||||
while(true) {
|
||||
size_t readBytes = fread(buffer,1,BUF_SIZE,stdin);
|
||||
decLen += readBytes;
|
||||
@ -99,7 +94,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
} else {
|
||||
// Open input file
|
||||
FILE *inFile = fopen(inputFile,"rb");
|
||||
if (inFile == NULL || outFile == NULL) {
|
||||
if (inFile == NULL) {
|
||||
fprintf(stderr,ERR"Could not open input file!\n");
|
||||
return 1;
|
||||
}
|
||||
@ -126,8 +121,8 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
}
|
||||
|
||||
size_t encLen = decLen + crypto_secretbox_MACBYTES;
|
||||
fprintf(stderr,OK"File is %d bytes.\n",decLen);
|
||||
fprintf(stderr,OK"Encoding file to %d bytes.\n", encLen);
|
||||
fprintf(stderr,OK"File is %zu bytes.\n",decLen);
|
||||
fprintf(stderr,OK"Encoding file to %zu bytes.\n", encLen);
|
||||
fprintf(stderr,NOR"Beginning encryption...\n");
|
||||
|
||||
// Generate a nonce, write headers and encrypted message
|
||||
@ -251,8 +246,8 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
return 1;
|
||||
}
|
||||
|
||||
fprintf(stderr,DBG"Size of encrypted file: %d bytes.\n",inLen);
|
||||
fprintf(stderr,DBG"This means the encrypted content is %d bytes.\n",encLen);
|
||||
fprintf(stderr,DBG"Size of encrypted file: %zu bytes.\n",inLen);
|
||||
fprintf(stderr,DBG"This means the encrypted content is %zu bytes.\n",encLen);
|
||||
|
||||
unsigned char encrypted[encLen];
|
||||
|
||||
@ -320,6 +315,38 @@ bool isFile(const char* filename) {
|
||||
return (stat(filename, &buffer) == 0);
|
||||
}
|
||||
|
||||
bool getPassword(char *pw, int size) {
|
||||
int i = 0;
|
||||
char c;
|
||||
// Hide input
|
||||
printf("\033[8m");
|
||||
pw[0] = '\0';
|
||||
// Get password from stdin
|
||||
while (true) {
|
||||
c = fgetc(stdin);
|
||||
if (c == '\r' || c == '\n' || feof(stdin)) {
|
||||
break;
|
||||
}
|
||||
// Return false on excess characters
|
||||
if (i < size - 1) {
|
||||
pw[i] = c;
|
||||
pw[i + 1] = '\0';
|
||||
} else
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
// Position cursor and print asterisks
|
||||
printf ("\033[0A");
|
||||
printf ("\033[32C");
|
||||
while (i) {
|
||||
printf ( "*");
|
||||
i--;
|
||||
}
|
||||
// Show input
|
||||
printf ("\033[28m");
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
fprintf(stderr,"- NetPaws Salty - File Encryption Program\n");
|
||||
fprintf(stderr,"- 2024 Ignacio Rivero\n\n");
|
||||
@ -370,7 +397,7 @@ int main(int argc, char *argv[]) {
|
||||
exit = 1;
|
||||
} else if (pass == -1) {
|
||||
if (strlen(argv[i+1]) > 128) {
|
||||
fprintf(stderr,ERR"Password too long.");
|
||||
fprintf(stderr,ERR"Password is too long.");
|
||||
exit = 1;
|
||||
} else {
|
||||
pass = ++i;
|
||||
@ -394,7 +421,6 @@ int main(int argc, char *argv[]) {
|
||||
// Set input and output filenames, or go to standard I/O if none
|
||||
char input[FILENAME_MAX];
|
||||
char output[FILENAME_MAX];
|
||||
char psw[129];
|
||||
|
||||
if (in == -1) {
|
||||
fprintf(stderr,NOR"No input file, reading from stdin.\n");
|
||||
@ -403,7 +429,7 @@ int main(int argc, char *argv[]) {
|
||||
fprintf(stderr,NOR"Reading from stdin.\n");
|
||||
stdinput = true;
|
||||
} else {
|
||||
snprintf(input,sizeof(input),argv[in]);
|
||||
snprintf(input,sizeof(input),"%s", argv[in]);
|
||||
}
|
||||
|
||||
if (out == -1) {
|
||||
@ -413,7 +439,7 @@ int main(int argc, char *argv[]) {
|
||||
fprintf(stderr,NOR"Writing to stdout.\n");
|
||||
snprintf(output,sizeof(output),"/dev/stdout");
|
||||
} else {
|
||||
snprintf(output,sizeof(output),argv[out]);
|
||||
snprintf(output,sizeof(output),"%s", argv[out]);
|
||||
}
|
||||
|
||||
char* password;
|
||||
@ -425,7 +451,12 @@ int main(int argc, char *argv[]) {
|
||||
fprintf(stderr, ERR"Cannot read from stdin without a password.\n");
|
||||
return 1;
|
||||
} else {
|
||||
password = getpass("Enter your encryption password: ");
|
||||
password = malloc(129);
|
||||
fprintf(stderr,ERR"Enter your encryption password: ");
|
||||
if (!getPassword(password,130)) {
|
||||
fprintf(stderr,ERR"Password is too long.");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (decrypt) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user