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 = gcc
|
||||||
cc ./build/salty.o -o ./build/salty -lsodium -I.
|
CFLAGS = -Wall -Wextra -std=c99 -O2
|
||||||
rm ./build/salty.o
|
LDFLAGS = -lsodium
|
||||||
|
BUILD_DIR = build
|
||||||
|
PREFIX = /usr/local
|
||||||
|
|
||||||
salty.o: build
|
all: $(BUILD_DIR)/salty
|
||||||
cc -c salty.c -o ./build/salty.o -lsodium -I.
|
|
||||||
|
|
||||||
build:
|
$(BUILD_DIR)/salty: salty.c pawstd.h | $(BUILD_DIR)
|
||||||
mkdir -p ./build
|
$(CC) $(CFLAGS) -o $@ salty.c $(LDFLAGS)
|
||||||
|
|
||||||
|
$(BUILD_DIR):
|
||||||
|
mkdir -p $(BUILD_DIR)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD_DIR)
|
||||||
|
|
||||||
install:
|
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
|
||||||
75
salty.c
75
salty.c
@ -2,12 +2,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sodium.h>
|
#include <sodium.h>
|
||||||
#include <pawstd.h>
|
#include "pawstd.h"
|
||||||
#define KEY_SIZE crypto_secretbox_KEYBYTES
|
#define KEY_SIZE crypto_secretbox_KEYBYTES
|
||||||
#define SALT_SIZE crypto_pwhash_SALTBYTES
|
#define SALT_SIZE crypto_pwhash_SALTBYTES
|
||||||
#define BUF_SIZE 1024
|
#define BUF_SIZE 1024
|
||||||
@ -69,9 +66,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
|||||||
fclose(outFile);
|
fclose(outFile);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int i = 0;
|
while(true) {
|
||||||
|
|
||||||
while(true) {
|
|
||||||
size_t readBytes = fread(buffer,1,BUF_SIZE,stdin);
|
size_t readBytes = fread(buffer,1,BUF_SIZE,stdin);
|
||||||
decLen += readBytes;
|
decLen += readBytes;
|
||||||
// Prevents reallocating to zero bytes.
|
// Prevents reallocating to zero bytes.
|
||||||
@ -99,7 +94,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
|||||||
} else {
|
} else {
|
||||||
// Open input file
|
// Open input file
|
||||||
FILE *inFile = fopen(inputFile,"rb");
|
FILE *inFile = fopen(inputFile,"rb");
|
||||||
if (inFile == NULL || outFile == NULL) {
|
if (inFile == NULL) {
|
||||||
fprintf(stderr,ERR"Could not open input file!\n");
|
fprintf(stderr,ERR"Could not open input file!\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -126,8 +121,8 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t encLen = decLen + crypto_secretbox_MACBYTES;
|
size_t encLen = decLen + crypto_secretbox_MACBYTES;
|
||||||
fprintf(stderr,OK"File is %d bytes.\n",decLen);
|
fprintf(stderr,OK"File is %zu bytes.\n",decLen);
|
||||||
fprintf(stderr,OK"Encoding file to %d bytes.\n", encLen);
|
fprintf(stderr,OK"Encoding file to %zu bytes.\n", encLen);
|
||||||
fprintf(stderr,NOR"Beginning encryption...\n");
|
fprintf(stderr,NOR"Beginning encryption...\n");
|
||||||
|
|
||||||
// Generate a nonce, write headers and encrypted message
|
// 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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr,DBG"Size of encrypted file: %d bytes.\n",inLen);
|
fprintf(stderr,DBG"Size of encrypted file: %zu bytes.\n",inLen);
|
||||||
fprintf(stderr,DBG"This means the encrypted content is %d bytes.\n",encLen);
|
fprintf(stderr,DBG"This means the encrypted content is %zu bytes.\n",encLen);
|
||||||
|
|
||||||
unsigned char encrypted[encLen];
|
unsigned char encrypted[encLen];
|
||||||
|
|
||||||
@ -320,6 +315,38 @@ bool isFile(const char* filename) {
|
|||||||
return (stat(filename, &buffer) == 0);
|
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[]) {
|
int main(int argc, char *argv[]) {
|
||||||
fprintf(stderr,"- NetPaws Salty - File Encryption Program\n");
|
fprintf(stderr,"- NetPaws Salty - File Encryption Program\n");
|
||||||
fprintf(stderr,"- 2024 Ignacio Rivero\n\n");
|
fprintf(stderr,"- 2024 Ignacio Rivero\n\n");
|
||||||
@ -370,7 +397,7 @@ int main(int argc, char *argv[]) {
|
|||||||
exit = 1;
|
exit = 1;
|
||||||
} else if (pass == -1) {
|
} else if (pass == -1) {
|
||||||
if (strlen(argv[i+1]) > 128) {
|
if (strlen(argv[i+1]) > 128) {
|
||||||
fprintf(stderr,ERR"Password too long.");
|
fprintf(stderr,ERR"Password is too long.");
|
||||||
exit = 1;
|
exit = 1;
|
||||||
} else {
|
} else {
|
||||||
pass = ++i;
|
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
|
// Set input and output filenames, or go to standard I/O if none
|
||||||
char input[FILENAME_MAX];
|
char input[FILENAME_MAX];
|
||||||
char output[FILENAME_MAX];
|
char output[FILENAME_MAX];
|
||||||
char psw[129];
|
|
||||||
|
|
||||||
if (in == -1) {
|
if (in == -1) {
|
||||||
fprintf(stderr,NOR"No input file, reading from stdin.\n");
|
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");
|
fprintf(stderr,NOR"Reading from stdin.\n");
|
||||||
stdinput = true;
|
stdinput = true;
|
||||||
} else {
|
} else {
|
||||||
snprintf(input,sizeof(input),argv[in]);
|
snprintf(input,sizeof(input),"%s", argv[in]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (out == -1) {
|
if (out == -1) {
|
||||||
@ -413,7 +439,7 @@ int main(int argc, char *argv[]) {
|
|||||||
fprintf(stderr,NOR"Writing to stdout.\n");
|
fprintf(stderr,NOR"Writing to stdout.\n");
|
||||||
snprintf(output,sizeof(output),"/dev/stdout");
|
snprintf(output,sizeof(output),"/dev/stdout");
|
||||||
} else {
|
} else {
|
||||||
snprintf(output,sizeof(output),argv[out]);
|
snprintf(output,sizeof(output),"%s", argv[out]);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* password;
|
char* password;
|
||||||
@ -422,13 +448,18 @@ int main(int argc, char *argv[]) {
|
|||||||
if (pass > 0) {
|
if (pass > 0) {
|
||||||
password = argv[pass];
|
password = argv[pass];
|
||||||
} else if (in < 0) {
|
} else if (in < 0) {
|
||||||
fprintf(stderr,ERR"Cannot read from stdin without a password.\n");
|
fprintf(stderr, ERR"Cannot read from stdin without a password.\n");
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} 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) {
|
if (decrypt) {
|
||||||
return decryptMessage(input,output,password);
|
return decryptMessage(input,output,password);
|
||||||
} else {
|
} else {
|
||||||
return encryptMessage(input,output,password);
|
return encryptMessage(input,output,password);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user