Skip to content

Commit

Permalink
Add hash and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
skwarq committed Mar 2, 2024
1 parent 2167911 commit dc791b8
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 3 deletions.
6 changes: 3 additions & 3 deletions encrypt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ cmake_minimum_required(VERSION 3.14)
# # Basic Files
file(GLOB project_src
"symCrypt/*.cpp"
"hash/*.cpp"
)

project(encrpyt)

option(ENCRPYT_CPP_BUILD_STANDALONE "Set if you wish to fetch all dependencies and build standalone unit tests." OFF)
option(USE_SYSTEM_OPENSSL "Use the system's OpenSSL if available" OFF)

include(FindPackageHandleStandardArgs)
Expand Down Expand Up @@ -56,7 +56,7 @@ target_link_libraries(encrpyt PUBLIC OpenSSL::SSL OpenSSL::Crypto)
target_include_directories(encrpyt PUBLIC "../")
target_compile_options(encrpyt PRIVATE -Wall -Wextra -Wpedantic -Werror)

if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING) AND(BUILD_TESTING))
# if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING) AND(BUILD_TESTING))
enable_testing()
add_subdirectory(uTests)
endif()
# endif()
57 changes: 57 additions & 0 deletions encrypt/README.md
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;
}
```
6 changes: 6 additions & 0 deletions encrypt/hash/FakeHash.cpp
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());
}
10 changes: 10 additions & 0 deletions encrypt/hash/FakeHash.hpp
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
11 changes: 11 additions & 0 deletions encrypt/hash/IHash.hpp
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
31 changes: 31 additions & 0 deletions encrypt/hash/SHA512.cpp
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());
}
10 changes: 10 additions & 0 deletions encrypt/hash/SHA512.hpp
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
1 change: 1 addition & 0 deletions encrypt/symCrypt/ISymCipher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace encrypt {
struct ISymCipher {
virtual ~ISymCipher() = default;
virtual void encrypt(const std::string &key,
std::span<const unsigned char> plainText,
std::span<unsigned char> &cipherText) const = 0;
Expand Down
21 changes: 21 additions & 0 deletions encrypt/uTests/symCrypt/SHA512Tests.cpp
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()));
}

0 comments on commit dc791b8

Please sign in to comment.