-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
150 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# encrypt | ||
|
||
`encrypt` is a straightforward wrapper library built on top of the OpenSSL library, offering implementations of cryptographic algorithms such as SHA-512 hashing and AES-256-CBC encryption. | ||
|
||
## Features | ||
|
||
- Efficient implementation of SHA-512 hashing. | ||
- Implementation of AES-256-CBC encryption with OpenSSL. | ||
- Utilizes `std::span` for handling input and output data efficiently. | ||
|
||
## Usage | ||
|
||
### SHA512 | ||
|
||
To compute the SHA-512 hash of a message, you can use the `SHA512` class: | ||
|
||
```cpp | ||
#include "encrypt/hash/SHA512.hpp" | ||
|
||
using namespace encrypt; | ||
|
||
int main() { | ||
std::string message = "Hello, World!"; | ||
SHA512 sha512; | ||
std::string hash = sha512.hash({message.begin(), message.end()}); | ||
std::cout << "SHA-512 hash of '" << message << "': " << hash << std::endl; | ||
return 0; | ||
} | ||
``` | ||
### AES256CBC | ||
To perform AES-256-CBC encryption and decryption, you can use the `AES256CBC` class: | ||
|
||
```cpp | ||
#include "encrypt/symCrypt/AES256CBC.hpp" | ||
|
||
using namespace encrypt; | ||
|
||
int main() { | ||
std::string key = "YourKeyHere"; | ||
std::string iv = "YourInitializationVectorHere"; | ||
AES256CBC aes256cbc({iv.begin(), iv.end()}); | ||
|
||
std::string plaintext = "YourPlainTextHere"; | ||
std::string ciphertext(plaintext.size() + AES_BLOCK_SIZE, '\0'); | ||
|
||
// Encryption | ||
aes256cbc.encrypt(key, {plaintext.begin(), plaintext.end()}, {ciphertext.begin(), ciphertext.end()}); | ||
std::cout << "Encrypted text: " << ciphertext << std::endl; | ||
|
||
// Decryption | ||
std::string decryptedtext(plaintext.size(), '\0'); | ||
aes256cbc.decrypt(key, {ciphertext.begin(), ciphertext.end()}, {decryptedtext.begin(), decryptedtext.end()}); | ||
std::cout << "Decrypted text: " << decryptedtext << std::endl; | ||
|
||
return 0; | ||
} | ||
``` |
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,6 @@ | ||
#include "FakeHash.hpp" | ||
using namespace encrypt; | ||
|
||
std::string FakeHash::hash(std::span<const unsigned char> input) const { | ||
return std::string(input.begin(), input.end()); | ||
} |
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,10 @@ | ||
#pragma once | ||
|
||
#include "IHash.hpp" | ||
|
||
namespace encrypt { | ||
class FakeHash : public IHash { | ||
public: | ||
std::string hash(std::span<const unsigned char> input) const override; | ||
}; | ||
} // namespace encrypt |
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,11 @@ | ||
#pragma once | ||
#include <span> | ||
#include <string> | ||
|
||
namespace encrypt { | ||
class IHash { | ||
public: | ||
virtual ~IHash() = default; | ||
virtual std::string hash(std::span<const unsigned char> input) const = 0; | ||
}; | ||
} // namespace encrypt |
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,31 @@ | ||
#include "SHA512.hpp" | ||
|
||
#include <openssl/evp.h> | ||
#include <openssl/sha.h> | ||
|
||
#include <memory> | ||
#include <stdexcept> | ||
#include <vector> | ||
|
||
using namespace encrypt; | ||
|
||
using EVP_MD_CTX_ptr = | ||
std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>; | ||
|
||
std::string SHA512::hash(std::span<const unsigned char> input) const { | ||
EVP_MD_CTX_ptr ctx(EVP_MD_CTX_new(), ::EVP_MD_CTX_free); | ||
if (!ctx) { | ||
throw std::runtime_error("Error creating EVP_MD_CTX"); | ||
} | ||
if (!EVP_DigestInit_ex(ctx.get(), EVP_sha512(), nullptr)) { | ||
throw std::runtime_error("Error initializing SHA-512 digest"); | ||
} | ||
if (!EVP_DigestUpdate(ctx.get(), input.data(), input.size())) { | ||
throw std::runtime_error("Error updating SHA-512 digest"); | ||
} | ||
std::vector<unsigned char> hash(SHA512_DIGEST_LENGTH); | ||
if (!EVP_DigestFinal_ex(ctx.get(), hash.data(), nullptr)) { | ||
throw std::runtime_error("Error finalizing SHA-512 digest"); | ||
} | ||
return std::string(hash.begin(), hash.end()); | ||
} |
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,10 @@ | ||
#pragma once | ||
|
||
#include "IHash.hpp" | ||
|
||
namespace encrypt { | ||
class SHA512 : public IHash { | ||
public: | ||
std::string hash(std::span<const unsigned char> input) const override; | ||
}; | ||
} // namespace encrypt |
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,21 @@ | ||
#include <openssl/rand.h> | ||
|
||
#include "encrypt/hash/SHA512.hpp" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace encrypt; | ||
|
||
TEST(SHA512Tests, simpleHash) { | ||
std::vector<unsigned char> input(1024); | ||
if (RAND_bytes(input.data(), input.size()) != 1) { | ||
throw std::runtime_error("Error generating"); | ||
} | ||
SHA512 hash; | ||
|
||
auto s1 = hash.hash({input.data(), input.size()}); | ||
auto s2 = hash.hash({input.data(), input.size()}); | ||
auto s3 = hash.hash({input.data(), input.size()}); | ||
EXPECT_EQ(s1, s2); | ||
EXPECT_EQ(s1, s3); | ||
EXPECT_NE(s1, std::string(input.begin(), input.end())); | ||
} |