-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: implement encryption for logfiles
- Loading branch information
1 parent
eec51a1
commit 0333a67
Showing
12 changed files
with
309 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include "crypto.hpp" | ||
|
||
#include <openssl/evp.h> | ||
#include <openssl/rand.h> | ||
|
||
namespace clog::utils { | ||
|
||
std::string encryptFile(const std::string& password, std::istream& file) { | ||
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); | ||
if (!ctx) { | ||
throw std::runtime_error{"Encryption failed!"}; | ||
} | ||
|
||
const EVP_CIPHER* cipher = EVP_aes_128_cfb(); // You can change the cipher if needed | ||
unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
|
||
if (EVP_BytesToKey(cipher, EVP_md5(), NULL, reinterpret_cast<const unsigned char*>(password.c_str()), password.size(), 1, key, iv) != 16) { | ||
EVP_CIPHER_CTX_free(ctx); | ||
throw std::runtime_error{"Encryption failed!"}; | ||
} | ||
|
||
if (EVP_EncryptInit_ex(ctx, cipher, nullptr, key, iv) != 1) { | ||
EVP_CIPHER_CTX_free(ctx); | ||
throw std::runtime_error{"Encryption failed!"}; | ||
} | ||
|
||
unsigned char inBuffer[1024]; | ||
unsigned char outBuffer[1024 + EVP_MAX_BLOCK_LENGTH]; | ||
int bytesRead = 0; | ||
int outLength = 0; | ||
|
||
std::string output; | ||
|
||
while (file.good()) { | ||
file.read(reinterpret_cast<char*>(inBuffer), sizeof(inBuffer)); | ||
bytesRead = static_cast<int>(file.gcount()); | ||
|
||
if (EVP_EncryptUpdate(ctx, outBuffer, &outLength, inBuffer, bytesRead) != 1) { | ||
EVP_CIPHER_CTX_free(ctx); | ||
throw std::runtime_error{"Encryption failed!"}; | ||
} | ||
|
||
output += std::string{outBuffer, outBuffer + outLength}; | ||
} | ||
|
||
if (EVP_EncryptFinal_ex(ctx, outBuffer, &outLength) != 1) { | ||
EVP_CIPHER_CTX_free(ctx); | ||
throw std::runtime_error{"Encryption failed!"}; | ||
} | ||
output += std::string{outBuffer, outBuffer + outLength}; | ||
EVP_CIPHER_CTX_free(ctx); | ||
|
||
return output; | ||
} | ||
|
||
|
||
std::string decryptFile(const std::string& password, std::istream& file) { | ||
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); | ||
if (!ctx) { | ||
EVP_CIPHER_CTX_free(ctx); | ||
throw std::runtime_error{"Encryption failed!"}; | ||
} | ||
|
||
const EVP_CIPHER* cipher = EVP_aes_128_cfb(); | ||
unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
|
||
if (EVP_BytesToKey(cipher, EVP_md5(), NULL, reinterpret_cast<const unsigned char*>(password.c_str()), password.size(), 1, key, iv) != 16) { | ||
EVP_CIPHER_CTX_free(ctx); | ||
throw std::runtime_error{"Encryption failed!"}; | ||
} | ||
|
||
if (EVP_DecryptInit_ex(ctx, cipher, nullptr, key, iv) != 1) { | ||
EVP_CIPHER_CTX_free(ctx); | ||
throw std::runtime_error{"Encryption failed!"}; | ||
} | ||
|
||
unsigned char inBuffer[1024 + EVP_MAX_BLOCK_LENGTH]; | ||
unsigned char outBuffer[1024]; | ||
int bytesRead = 0; | ||
int outLength = 0; | ||
std::string output; | ||
|
||
while (file.good()) { | ||
file.read(reinterpret_cast<char*>(inBuffer), sizeof(inBuffer)); | ||
bytesRead = static_cast<int>(file.gcount()); | ||
|
||
if (EVP_DecryptUpdate(ctx, outBuffer, &outLength, inBuffer, bytesRead) != 1) { | ||
EVP_CIPHER_CTX_free(ctx); | ||
throw std::runtime_error{"Encryption failed!"}; | ||
} | ||
|
||
output += std::string{outBuffer, outBuffer + outLength}; | ||
} | ||
|
||
if (EVP_DecryptFinal_ex(ctx, outBuffer, &outLength) != 1) { | ||
EVP_CIPHER_CTX_free(ctx); | ||
throw std::runtime_error{"Encryption failed!"}; | ||
} | ||
|
||
output += std::string{outBuffer, outBuffer + outLength}; | ||
|
||
EVP_CIPHER_CTX_free(ctx); | ||
|
||
return output; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <istream> | ||
|
||
namespace clog::utils { | ||
|
||
std::string encryptFile(const std::string& password, std::istream& file); | ||
std::string decryptFile(const std::string& password, std::istream& file); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.