From fbd4f0695b708c175a59bf22917d15c91382397c Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 7 Jul 2020 10:37:08 +0800 Subject: [PATCH] add --- include/communicate/tcp_server.h | 16 +++++++++++++ include/type.h | 2 -- include/utils/aes_cbc_encryptor.h | 11 ++++----- include/utils/sha256_generator.h | 38 ++++++++++++++----------------- src/communicate/CMakeLists.txt | 5 ++++ src/communicate/tcp_server.cpp | 5 ++++ src/utils/CMakeLists.txt | 4 +++- src/utils/aes_cbc_encryptor.cpp | 9 ++++++++ src/utils/sha256_generator.cpp | 24 +++++++++++++++++-- test/test_util/test_sha.cpp | 20 ++++++++++++++++ 10 files changed, 101 insertions(+), 33 deletions(-) create mode 100644 include/communicate/tcp_server.h create mode 100644 src/communicate/CMakeLists.txt create mode 100644 src/communicate/tcp_server.cpp create mode 100644 test/test_util/test_sha.cpp diff --git a/include/communicate/tcp_server.h b/include/communicate/tcp_server.h new file mode 100644 index 0000000..7a25365 --- /dev/null +++ b/include/communicate/tcp_server.h @@ -0,0 +1,16 @@ +// +// Created by 胡宇 on 2020/7/7. +// + +#ifndef NET_TCP_SERVER_H +#define NET_TCP_SERVER_H + +namespace Net { + + class TCPServer { + + }; + +} + +#endif //NET_TCP_SERVER_H diff --git a/include/type.h b/include/type.h index 4f95d2f..7876a01 100644 --- a/include/type.h +++ b/include/type.h @@ -51,8 +51,6 @@ #include #include -#include - using std::string; using std::vector; diff --git a/include/utils/aes_cbc_encryptor.h b/include/utils/aes_cbc_encryptor.h index 6777f88..8eb1a3b 100644 --- a/include/utils/aes_cbc_encryptor.h +++ b/include/utils/aes_cbc_encryptor.h @@ -19,14 +19,9 @@ namespace Net { class AESCBCEncryptor { public: - AESCBCEncryptor() { - generate_random_key_data(); - aes_init(key_data); - } + AESCBCEncryptor(); - string getKeyData() const{ - return key_data; - } + string getKeyData() const; void encrypt(const std::string &data, std::string &encrypted_data); @@ -34,7 +29,9 @@ namespace Net { private: const int nrounds = 8; + uint8_t key[32], iv[32]; + EVP_CIPHER_CTX *e_ctx = EVP_CIPHER_CTX_new(); std::string key_data; diff --git a/include/utils/sha256_generator.h b/include/utils/sha256_generator.h index bbaf89f..ac988c0 100644 --- a/include/utils/sha256_generator.h +++ b/include/utils/sha256_generator.h @@ -9,32 +9,28 @@ using std::string; using std::ifstream; using std::stringstream; +namespace Net { + /** * The Generator of SHA256 Hex * We can use it in an easy way. */ -class SHA256Generator{ -public: - SHA256Generator(string data){ - this->raw_data = std::move(data); - } + class SHA256Generator { + public: + SHA256Generator(string data); - SHA256Generator(ifstream stream){ - while(stream.good()){ - stream >> raw_data; - } - stream.close(); - } + SHA256Generator(ifstream stream); - void generate(); + void replace(string &str); - string getHex(){ - if(!if_generate) generate(); - return this->sha256_data; - } + void generate(); -private: - bool if_generate = false; - string raw_data; - string sha256_data; -}; + string getHex(); + + private: + bool if_generate = false; + string raw_data; + string sha256_data; + }; + +} \ No newline at end of file diff --git a/src/communicate/CMakeLists.txt b/src/communicate/CMakeLists.txt new file mode 100644 index 0000000..78a9530 --- /dev/null +++ b/src/communicate/CMakeLists.txt @@ -0,0 +1,5 @@ +file(GLOB commuSrc *.cpp) + +add_library(commu STATIC ${commuSrc}) + +target_link_libraries(commu utils debugTools) \ No newline at end of file diff --git a/src/communicate/tcp_server.cpp b/src/communicate/tcp_server.cpp new file mode 100644 index 0000000..7dc2ca8 --- /dev/null +++ b/src/communicate/tcp_server.cpp @@ -0,0 +1,5 @@ +// +// Created by 胡宇 on 2020/7/7. +// + +#include "communicate/tcp_server.h" diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 6d39b9b..0fd6aa0 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,3 +1,5 @@ -add_library(utils STATIC rsa_key_chain.cpp aes_cbc_encryptor.cpp random_generator.cpp) +file(GLOB utilSrc *.cpp) + +add_library(utils STATIC ${utilSrc}) target_link_libraries(utils debugTools ssl crypto) \ No newline at end of file diff --git a/src/utils/aes_cbc_encryptor.cpp b/src/utils/aes_cbc_encryptor.cpp index bcf627c..33c06e9 100644 --- a/src/utils/aes_cbc_encryptor.cpp +++ b/src/utils/aes_cbc_encryptor.cpp @@ -66,3 +66,12 @@ void Net::AESCBCEncryptor::aes_init(string &key_data) { EVP_CIPHER_CTX_init(e_ctx); EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv); } + +string Net::AESCBCEncryptor::getKeyData() const { + return key_data; +} + +Net::AESCBCEncryptor::AESCBCEncryptor() { + generate_random_key_data(); + aes_init(key_data); +} diff --git a/src/utils/sha256_generator.cpp b/src/utils/sha256_generator.cpp index cd75f18..09d3d91 100644 --- a/src/utils/sha256_generator.cpp +++ b/src/utils/sha256_generator.cpp @@ -1,10 +1,10 @@ #include "type.h" -#include "util/sha256_generator.h" +#include "utils/sha256_generator.h" /** * Generate SHA256 Hex Code */ -void SHA256Generator::generate() { +void Net::SHA256Generator::generate() { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); @@ -19,3 +19,23 @@ void SHA256Generator::generate() { sha256_data = buffer.str(); if_generate = true; } + +void Net::SHA256Generator::replace(string &str) { + this->raw_data = str; +} + +string Net::SHA256Generator::getHex() { + if (!if_generate) generate(); + return this->sha256_data; +} + +Net::SHA256Generator::SHA256Generator(ifstream stream) { + while (stream.good()) { + stream >> raw_data; + } + stream.close(); +} + +Net::SHA256Generator::SHA256Generator(string data) { + this->raw_data = std::move(data); +} diff --git a/test/test_util/test_sha.cpp b/test/test_util/test_sha.cpp new file mode 100644 index 0000000..b213fe6 --- /dev/null +++ b/test/test_util/test_sha.cpp @@ -0,0 +1,20 @@ +// +// Created by 胡宇 on 2020/7/7. +// + +#include + +#include "utils/sha256_generator.h" +#include "debug_tools/print_tools.h" + +using namespace Net; + +TEST(SHA256Test, base_test_1){ + SHA256Generator generator("Hello World."); + generator.generate(); + + PrintTools::printInfo(generator.getHex(), "SHA256 Hex"); + + ASSERT_EQ(generator.getHex(), + std::string("f4bb1975bf1f81f76ce824f7536c1e101a8060a632a52289d530a6f600d52c92")); +} \ No newline at end of file