This commit is contained in:
eric 2020-07-07 10:37:08 +08:00
parent 7ed517a561
commit fbd4f0695b
10 changed files with 101 additions and 33 deletions

View File

@ -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

View File

@ -51,8 +51,6 @@
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include <SQLiteCpp/SQLiteCpp.h>
using std::string;
using std::vector;

View File

@ -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;

View File

@ -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;
};
}

View File

@ -0,0 +1,5 @@
file(GLOB commuSrc *.cpp)
add_library(commu STATIC ${commuSrc})
target_link_libraries(commu utils debugTools)

View File

@ -0,0 +1,5 @@
//
// Created by 胡宇 on 2020/7/7.
//
#include "communicate/tcp_server.h"

View File

@ -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)

View File

@ -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);
}

View File

@ -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);
}

View File

@ -0,0 +1,20 @@
//
// Created by 胡宇 on 2020/7/7.
//
#include <gtest/gtest.h>
#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"));
}