Fixed password not replacing properly, and memory bug with large files apparently
This commit is contained in:
parent
a0c81e7064
commit
40f9b3dce8
35
salty.c
35
salty.c
@ -74,7 +74,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
unsigned char* old = input;
|
||||
input = realloc(input, decLen);
|
||||
if (input == NULL) {
|
||||
fprintf(stderr,ERR"Memory allocation error.\n");
|
||||
fprintf(stderr, ERR"Memory allocation error.\n");
|
||||
fclose(outFile);
|
||||
free(old);
|
||||
return 1;
|
||||
@ -121,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 %zu bytes.\n",decLen);
|
||||
fprintf(stderr,OK"Encoding file to %zu bytes.\n", encLen);
|
||||
fprintf(stderr,OK"Input file is %zu bytes.\n",decLen);
|
||||
fprintf(stderr,OK"Encrypting file to %zu bytes.\n", encLen);
|
||||
fprintf(stderr,NOR"Beginning encryption...\n");
|
||||
|
||||
// Generate a nonce, write headers and encrypted message
|
||||
@ -130,7 +130,13 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
randombytes_buf(nonce,sizeof(nonce));
|
||||
|
||||
// Encrypt the data
|
||||
unsigned char encrypted[encLen];
|
||||
unsigned char* encrypted = malloc(encLen);
|
||||
if (encrypted == NULL) {
|
||||
fprintf(stderr,ERR"Memory allocation error: File too large.\n");
|
||||
free(decrypted);
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
crypto_secretbox_easy(encrypted, decrypted, decLen, nonce, key);
|
||||
|
||||
// Patchy but correct-ish attempt to solve annoying stdout issue
|
||||
@ -152,7 +158,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
memcpy(outputBuffer + offset, nonce, sizeof(nonce));
|
||||
offset += sizeof(nonce);
|
||||
|
||||
memcpy(outputBuffer + offset, encrypted, sizeof(encrypted));
|
||||
memcpy(outputBuffer + offset, encrypted, encLen);
|
||||
|
||||
fprintf(stderr,NOR"Writing encrypted data...\n");
|
||||
|
||||
@ -246,8 +252,8 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
return 1;
|
||||
}
|
||||
|
||||
fprintf(stderr,DBG"Size of encrypted file: %zu bytes.\n",inLen);
|
||||
fprintf(stderr,DBG"This means the encrypted content is %zu bytes.\n",encLen);
|
||||
fprintf(stderr,OK"Size of input file: %zu bytes.\n",inLen);
|
||||
fprintf(stderr,OK"Encrypted content is %zu bytes.\n",encLen);
|
||||
|
||||
unsigned char encrypted[encLen];
|
||||
|
||||
@ -318,11 +324,12 @@ bool isFile(const char* filename) {
|
||||
bool getPassword(char *pw, int size) {
|
||||
int i = 0;
|
||||
char c;
|
||||
pw[0] = '\0';
|
||||
// Hide input
|
||||
printf("\033[8m");
|
||||
pw[0] = '\0';
|
||||
// Get password from stdin
|
||||
while (true) {
|
||||
// Overwrite password in console
|
||||
c = fgetc(stdin);
|
||||
if (c == '\r' || c == '\n' || feof(stdin)) {
|
||||
break;
|
||||
@ -335,15 +342,16 @@ bool getPassword(char *pw, int size) {
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
// Position cursor and print asterisks
|
||||
printf ("\033[0A");
|
||||
printf ("\033[32C");
|
||||
printf ("\033[40C");
|
||||
fflush(stdout);
|
||||
while (i) {
|
||||
printf ( "*");
|
||||
fprintf (stdout,"*");
|
||||
i--;
|
||||
}
|
||||
fflush(stderr);
|
||||
// Show input
|
||||
printf ("\033[28m");
|
||||
printf ("\033[28m\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -452,7 +460,8 @@ int main(int argc, char *argv[]) {
|
||||
return 1;
|
||||
} else {
|
||||
password = malloc(129);
|
||||
fprintf(stderr,ERR"Enter your encryption password: ");
|
||||
printf(NOR"Enter your encryption password: ");
|
||||
fflush(stdout);
|
||||
if (!getPassword(password,130)) {
|
||||
fprintf(stderr,ERR"Password is too long.");
|
||||
return 1;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user