secret:添加数据加密功能。
This commit is contained in:
parent
3a27934467
commit
334df57b63
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -30,7 +30,8 @@
|
|||||||
"cmake.configureArgs": [
|
"cmake.configureArgs": [
|
||||||
"-Wno-dev",
|
"-Wno-dev",
|
||||||
"-DCMAKE_PREFIX_PATH:STRING=C:/dev/wxwigets",
|
"-DCMAKE_PREFIX_PATH:STRING=C:/dev/wxwigets",
|
||||||
"-DUSE_GUI=ON"
|
"-DUSE_GUI=ON",
|
||||||
|
"-DUSE_TRANSM_TEST=ON"
|
||||||
],
|
],
|
||||||
"cmake.options.statusBarVisibility": "visible",
|
"cmake.options.statusBarVisibility": "visible",
|
||||||
"cmake.generator": "Ninja",
|
"cmake.generator": "Ninja",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
project(transm VERSION 1.4.1 LANGUAGES CXX)
|
project(transm VERSION 1.4.2 LANGUAGES CXX)
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
@ -57,6 +57,11 @@ add_subdirectory(client)
|
|||||||
add_subdirectory(filecomplete)
|
add_subdirectory(filecomplete)
|
||||||
add_subdirectory(tinyaes)
|
add_subdirectory(tinyaes)
|
||||||
|
|
||||||
|
if (DEFINED USE_TRANSM_TEST)
|
||||||
|
message(STATUS "USE USE_TRANSM_TEST ${USE_TRANSM_TEST}")
|
||||||
|
add_subdirectory(test)
|
||||||
|
endif()
|
||||||
|
|
||||||
string(TIMESTAMP VERSION_BUILD_DATE "%Y-%m-%d %H:%M")
|
string(TIMESTAMP VERSION_BUILD_DATE "%Y-%m-%d %H:%M")
|
||||||
execute_process(
|
execute_process(
|
||||||
COMMAND git rev-parse --short HEAD
|
COMMAND git rev-parse --short HEAD
|
||||||
|
@ -338,7 +338,7 @@ bool CClient::cmd_sub_list(const std::string& param)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(own_id_);
|
||||||
msg_info.uuid = uuid_;
|
msg_info.uuid = uuid_;
|
||||||
msg_info.str = msg;
|
msg_info.str = msg;
|
||||||
|
|
||||||
@ -426,7 +426,7 @@ bool CClient::cmd_upload_files(const std::string& param)
|
|||||||
list_file_ = "auto_list";
|
list_file_ = "auto_list";
|
||||||
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
||||||
buf->type_ = TYPE_REQUEST_UPDATE_LIST;
|
buf->type_ = TYPE_REQUEST_UPDATE_LIST;
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(own_id_);
|
||||||
msg_info.str = handel_ret;
|
msg_info.str = handel_ret;
|
||||||
serialize(msg_info, &buf->data_, buf->len_);
|
serialize(msg_info, &buf->data_, buf->len_);
|
||||||
buf->tid_ = clients_[index]->id;
|
buf->tid_ = clients_[index]->id;
|
||||||
@ -472,7 +472,7 @@ bool CClient::down_one_file(int remote_id, const std::string& file, const std::s
|
|||||||
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
||||||
buf->type_ = TYPE_OPEN_FILE;
|
buf->type_ = TYPE_OPEN_FILE;
|
||||||
buf->tid_ = client->id;
|
buf->tid_ = client->id;
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(own_id_);
|
||||||
msg_info.str = file;
|
msg_info.str = file;
|
||||||
serialize(msg_info, &buf->data_, buf->len_);
|
serialize(msg_info, &buf->data_, buf->len_);
|
||||||
if (!send_frame(buf.get())) {
|
if (!send_frame(buf.get())) {
|
||||||
@ -649,7 +649,7 @@ bool CClient::cmd_sub_task(const std::string& param, bool is_send)
|
|||||||
} else {
|
} else {
|
||||||
buf->type_ = TYPE_REQUEST_DOWN_UPDATE_LIST;
|
buf->type_ = TYPE_REQUEST_DOWN_UPDATE_LIST;
|
||||||
}
|
}
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(own_id_);
|
||||||
msg_info.str = handel_ret;
|
msg_info.str = handel_ret;
|
||||||
serialize(msg_info, &buf->data_, buf->len_);
|
serialize(msg_info, &buf->data_, buf->len_);
|
||||||
buf->tid_ = clients_[index]->id;
|
buf->tid_ = clients_[index]->id;
|
||||||
@ -757,7 +757,7 @@ bool CClient::cmd_ls(const std::string& param)
|
|||||||
const auto& sr = clients_[index];
|
const auto& sr = clients_[index];
|
||||||
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
||||||
buf->type_ = TYPE_GET_DIRFILES;
|
buf->type_ = TYPE_GET_DIRFILES;
|
||||||
CMessageInfo msg_info{};
|
CMessageInfo msg_info(own_id_);
|
||||||
msg_info.str = path;
|
msg_info.str = path;
|
||||||
serialize(msg_info, &buf->data_, buf->len_);
|
serialize(msg_info, &buf->data_, buf->len_);
|
||||||
buf->tid_ = sr->id;
|
buf->tid_ = sr->id;
|
||||||
@ -949,7 +949,7 @@ void CClient::get_id()
|
|||||||
{
|
{
|
||||||
auto* bf = new CFrameBuffer();
|
auto* bf = new CFrameBuffer();
|
||||||
bf->type_ = TYPE_GET_ID;
|
bf->type_ = TYPE_GET_ID;
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(own_id_);
|
||||||
msg_info.uuid = uuid_;
|
msg_info.uuid = uuid_;
|
||||||
serialize(msg_info, &bf->data_, bf->len_);
|
serialize(msg_info, &bf->data_, bf->len_);
|
||||||
send_frame(bf);
|
send_frame(bf);
|
||||||
@ -970,7 +970,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
|
|||||||
}
|
}
|
||||||
case TYPE_GET_LIST: {
|
case TYPE_GET_LIST: {
|
||||||
clients_.clear();
|
clients_.clear();
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(buf->fid_);
|
||||||
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
||||||
TLOGE("{} GetList deserialize failed.", __LINE__);
|
TLOGE("{} GetList deserialize failed.", __LINE__);
|
||||||
break;
|
break;
|
||||||
@ -1044,7 +1044,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TYPE_GET_DIRFILES: {
|
case TYPE_GET_DIRFILES: {
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(buf->fid_);
|
||||||
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
||||||
TLOGE("{} GetDirFiles deserialize failed.", __LINE__);
|
TLOGE("{} GetDirFiles deserialize failed.", __LINE__);
|
||||||
break;
|
break;
|
||||||
@ -1071,7 +1071,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TYPE_GET_DIRFILES_FAILED: {
|
case TYPE_GET_DIRFILES_FAILED: {
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(buf->fid_);
|
||||||
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
||||||
TLOGE("{} GetDirFiles deserialize failed.", __LINE__);
|
TLOGE("{} GetDirFiles deserialize failed.", __LINE__);
|
||||||
break;
|
break;
|
||||||
@ -1080,7 +1080,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TYPE_GET_DIRFILES_DONE: {
|
case TYPE_GET_DIRFILES_DONE: {
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(buf->fid_);
|
||||||
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
||||||
TLOGE("{} GetDirFiles deserialize failed.", __LINE__);
|
TLOGE("{} GetDirFiles deserialize failed.", __LINE__);
|
||||||
break;
|
break;
|
||||||
@ -1107,7 +1107,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
|
|||||||
}
|
}
|
||||||
case TYPE_OPEN_FILE: {
|
case TYPE_OPEN_FILE: {
|
||||||
std::string keys{};
|
std::string keys{};
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(buf->fid_);
|
||||||
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
||||||
TLOGE("{} OpenFile deserialize failed.", __LINE__);
|
TLOGE("{} OpenFile deserialize failed.", __LINE__);
|
||||||
break;
|
break;
|
||||||
@ -1172,7 +1172,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TYPE_REQUEST_DOWN_UPDATE_LIST: {
|
case TYPE_REQUEST_DOWN_UPDATE_LIST: {
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(buf->fid_);
|
||||||
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
||||||
TLOGE("{} GetList deserialize failed.", __LINE__);
|
TLOGE("{} GetList deserialize failed.", __LINE__);
|
||||||
break;
|
break;
|
||||||
@ -1194,7 +1194,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
|
|||||||
TLOGW("Update Busy......, Ignore {}", buf->fid_);
|
TLOGW("Update Busy......, Ignore {}", buf->fid_);
|
||||||
buf->type_ = TYPE_BUSY_UPDATE_LIST;
|
buf->type_ = TYPE_BUSY_UPDATE_LIST;
|
||||||
} else {
|
} else {
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(buf->fid_);
|
||||||
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
||||||
TLOGE("{} GetList deserialize failed.", __LINE__);
|
TLOGE("{} GetList deserialize failed.", __LINE__);
|
||||||
break;
|
break;
|
||||||
|
@ -135,7 +135,7 @@ void CTcpServer::trans_data(CFrameBuffer* buf)
|
|||||||
switch (buf->type_) {
|
switch (buf->type_) {
|
||||||
case TYPE_GET_LIST: {
|
case TYPE_GET_LIST: {
|
||||||
TLOGI("[{}] GetList.", buf->fid_);
|
TLOGI("[{}] GetList.", buf->fid_);
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(buf->fid_);
|
||||||
get_client_list(msg_info);
|
get_client_list(msg_info);
|
||||||
serialize(msg_info, &buf->data_, buf->len_);
|
serialize(msg_info, &buf->data_, buf->len_);
|
||||||
if (fcli && !send_frame(fcli->socket_, buf)) {
|
if (fcli && !send_frame(fcli->socket_, buf)) {
|
||||||
@ -144,7 +144,7 @@ void CTcpServer::trans_data(CFrameBuffer* buf)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TYPE_UP_LIST: {
|
case TYPE_UP_LIST: {
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info(buf->fid_);
|
||||||
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
if (!deserialize(buf->data_, buf->len_, msg_info)) {
|
||||||
TLOGE("{} GetList deserialize failed.", __LINE__);
|
TLOGE("{} GetList deserialize failed.", __LINE__);
|
||||||
break;
|
break;
|
||||||
@ -303,7 +303,7 @@ void CTcpServer::th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (frame->type_ == TYPE_GET_ID) {
|
if (frame->type_ == TYPE_GET_ID) {
|
||||||
CMessageInfo msg_info;
|
CMessageInfo msg_info("");
|
||||||
if (!deserialize(frame->data_, frame->len_, msg_info)) {
|
if (!deserialize(frame->data_, frame->len_, msg_info)) {
|
||||||
TLOGE("{} GetId deserialize failed.", __LINE__);
|
TLOGE("{} GetId deserialize failed.", __LINE__);
|
||||||
delete frame;
|
delete frame;
|
||||||
|
7
test/CMakeLists.txt
Normal file
7
test/CMakeLists.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
|
project(test LANGUAGES CXX)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
|
add_executable(test main.cxx)
|
||||||
|
target_link_libraries(test PRIVATE tinyaes trans_util)
|
21
test/main.cxx
Normal file
21
test/main.cxx
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include <aes.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <util.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::string key = "sss";
|
||||||
|
uint8_t ik[32]{};
|
||||||
|
hash(key.c_str(), ik);
|
||||||
|
|
||||||
|
char* msg = new char[256]{};
|
||||||
|
auto len = std::snprintf(msg + 12, 256, "%s", "hello world");
|
||||||
|
std::cout << encrypt(ik, (uint8_t*)msg, len + 12) << std::endl;
|
||||||
|
|
||||||
|
uint8_t ik2[32]{};
|
||||||
|
hash(key.c_str(), ik2);
|
||||||
|
|
||||||
|
std::cout << decrypt(ik2, (uint8_t*)msg, len + 12) << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -9,5 +9,5 @@ endif()
|
|||||||
|
|
||||||
set(SOURCES util.h util.cpp)
|
set(SOURCES util.h util.cpp)
|
||||||
add_library(trans_util STATIC ${SOURCES})
|
add_library(trans_util STATIC ${SOURCES})
|
||||||
target_link_libraries(trans_util PUBLIC ofen filecomplete)
|
target_link_libraries(trans_util PUBLIC ofen filecomplete tinyaes)
|
||||||
target_include_directories(trans_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
target_include_directories(trans_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
111
util/util.cpp
111
util/util.cpp
@ -1,12 +1,14 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
#include <aes.hpp>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <of_util.h>
|
#include <of_util.h>
|
||||||
|
#include <random>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
CTransProtocal::CTransProtocal() = default;
|
CTransProtocal::CTransProtocal() = default;
|
||||||
|
constexpr uint8_t kz = 12;
|
||||||
CTransProtocal::~CTransProtocal() = default;
|
CTransProtocal::~CTransProtocal() = default;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -134,23 +136,23 @@ CFrameBuffer::~CFrameBuffer()
|
|||||||
void serialize(const CMessageInfo& msg_info, char** out_buf, int& len)
|
void serialize(const CMessageInfo& msg_info, char** out_buf, int& len)
|
||||||
{
|
{
|
||||||
CMessageInfo info(msg_info);
|
CMessageInfo info(msg_info);
|
||||||
info.cmd = localtou8(info.cmd);
|
info.id = localtou8(info.id);
|
||||||
info.uuid = localtou8(info.uuid);
|
info.uuid = localtou8(info.uuid);
|
||||||
info.str = localtou8(info.str);
|
info.str = localtou8(info.str);
|
||||||
info.o = localtou8(info.o);
|
info.o = localtou8(info.o);
|
||||||
|
|
||||||
// 计算总长度
|
// 计算总长度
|
||||||
len = sizeof(int) * 4 + info.cmd.size() + info.uuid.size() + info.str.size() + info.o.size();
|
len = sizeof(int) * 4 + info.id.size() + info.uuid.size() + info.str.size() + info.o.size() + kz;
|
||||||
*out_buf = new char[len]; // 分配内存(调用方负责释放)
|
*out_buf = new char[len]; // 分配内存(调用方负责释放)
|
||||||
|
|
||||||
char* ptr = *out_buf;
|
char* ptr = *out_buf + kz;
|
||||||
|
|
||||||
// 序列化 cmd
|
// 序列化 cmd
|
||||||
int cmd_size = static_cast<int>(info.cmd.size());
|
int id_size = static_cast<int>(info.id.size());
|
||||||
memcpy(ptr, &cmd_size, sizeof(int));
|
memcpy(ptr, &id_size, sizeof(int));
|
||||||
ptr += sizeof(int);
|
ptr += sizeof(int);
|
||||||
memcpy(ptr, info.cmd.data(), cmd_size);
|
memcpy(ptr, info.id.data(), id_size);
|
||||||
ptr += cmd_size;
|
ptr += id_size;
|
||||||
|
|
||||||
// 序列化 uuid
|
// 序列化 uuid
|
||||||
int uuid_size = static_cast<int>(info.uuid.size());
|
int uuid_size = static_cast<int>(info.uuid.size());
|
||||||
@ -171,12 +173,28 @@ void serialize(const CMessageInfo& msg_info, char** out_buf, int& len)
|
|||||||
memcpy(ptr, &o_size, sizeof(int));
|
memcpy(ptr, &o_size, sizeof(int));
|
||||||
ptr += sizeof(int);
|
ptr += sizeof(int);
|
||||||
memcpy(ptr, info.o.data(), o_size);
|
memcpy(ptr, info.o.data(), o_size);
|
||||||
|
|
||||||
|
uint8_t ik[32]{};
|
||||||
|
hash(msg_info.id.c_str(), ik);
|
||||||
|
if (!encrypt(ik, (uint8_t*)(*out_buf), len)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool deserialize(const char* data, int len, CMessageInfo& msg_info)
|
bool deserialize(char* data, int len, CMessageInfo& msg_info)
|
||||||
{
|
{
|
||||||
CMessageInfo info;
|
if (len < kz) {
|
||||||
const char* ptr = data;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t ik[32]{};
|
||||||
|
hash(msg_info.id.c_str(), ik);
|
||||||
|
if (!decrypt(ik, (uint8_t*)(data), len)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
CMessageInfo info(msg_info.id);
|
||||||
|
char* ptr = data + kz;
|
||||||
int remaining = len;
|
int remaining = len;
|
||||||
|
|
||||||
// 反序列化 cmd
|
// 反序列化 cmd
|
||||||
@ -184,17 +202,17 @@ bool deserialize(const char* data, int len, CMessageInfo& msg_info)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_size;
|
int id_size;
|
||||||
memcpy(&cmd_size, ptr, sizeof(int));
|
memcpy(&id_size, ptr, sizeof(int));
|
||||||
ptr += sizeof(int);
|
ptr += sizeof(int);
|
||||||
remaining -= sizeof(int);
|
remaining -= sizeof(int);
|
||||||
if (remaining < cmd_size) {
|
if (remaining < id_size) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
info.cmd.assign(ptr, cmd_size);
|
info.id.assign(ptr, id_size);
|
||||||
ptr += cmd_size;
|
ptr += id_size;
|
||||||
remaining -= cmd_size;
|
remaining -= id_size;
|
||||||
|
|
||||||
// 反序列化 uuid
|
// 反序列化 uuid
|
||||||
if (remaining < static_cast<int>(sizeof(int))) {
|
if (remaining < static_cast<int>(sizeof(int))) {
|
||||||
@ -243,7 +261,7 @@ bool deserialize(const char* data, int len, CMessageInfo& msg_info)
|
|||||||
}
|
}
|
||||||
info.o.assign(ptr, o_size);
|
info.o.assign(ptr, o_size);
|
||||||
|
|
||||||
info.cmd = u8tolocal(info.cmd);
|
info.id = u8tolocal(info.id);
|
||||||
info.uuid = u8tolocal(info.uuid);
|
info.uuid = u8tolocal(info.uuid);
|
||||||
info.str = u8tolocal(info.str);
|
info.str = u8tolocal(info.str);
|
||||||
info.o = u8tolocal(info.o);
|
info.o = u8tolocal(info.o);
|
||||||
@ -270,12 +288,65 @@ std::string localtou8(const std::string& str)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void hash(const char* data, uint8_t k[32])
|
||||||
|
{
|
||||||
|
uint32_t h = 5381;
|
||||||
|
for (const char* p = data; *p; p++) {
|
||||||
|
h = ((h << 5) + h) + *p; // DJB2
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 32; i++) {
|
||||||
|
k[i] = (h >> (i % 4 * 8)) & 0xFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void rdm(uint8_t* o, size_t size)
|
||||||
|
{
|
||||||
|
std::random_device rd;
|
||||||
|
std::uniform_int_distribution<int> dist(0, 255);
|
||||||
|
std::generate(o, o + size, [&]() { return dist(rd); });
|
||||||
|
}
|
||||||
|
|
||||||
|
bool encrypt(const uint8_t* k, uint8_t* m, size_t len)
|
||||||
|
{
|
||||||
|
if (len < kz) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t nonce[kz]{};
|
||||||
|
rdm(nonce, sizeof(nonce));
|
||||||
|
memcpy(m, nonce, kz);
|
||||||
|
|
||||||
|
struct AES_ctx ctx;
|
||||||
|
AES_init_ctx_iv(&ctx, k, nonce);
|
||||||
|
AES_CTR_xcrypt_buffer(&ctx, m + kz, len - kz);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool decrypt(const uint8_t* k, uint8_t* m, size_t len)
|
||||||
|
{
|
||||||
|
if (len < kz) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t nonce[kz]{};
|
||||||
|
memcpy(nonce, m, kz);
|
||||||
|
|
||||||
|
struct AES_ctx ctx;
|
||||||
|
AES_init_ctx_iv(&ctx, k, nonce);
|
||||||
|
AES_CTR_xcrypt_buffer(&ctx, m + kz, len - kz);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
CMessageInfo::CMessageInfo(const std::string& id) : id(id)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
CMessageInfo::CMessageInfo(const CMessageInfo& info)
|
CMessageInfo::CMessageInfo(const CMessageInfo& info)
|
||||||
{
|
{
|
||||||
if (&info == this) {
|
if (&info == this) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cmd = info.cmd;
|
id = info.id;
|
||||||
uuid = info.uuid;
|
uuid = info.uuid;
|
||||||
str = info.str;
|
str = info.str;
|
||||||
o = info.o;
|
o = info.o;
|
||||||
@ -286,7 +357,7 @@ CMessageInfo& CMessageInfo::operator=(const CMessageInfo& info)
|
|||||||
if (&info == this) {
|
if (&info == this) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
cmd = info.cmd;
|
id = info.id;
|
||||||
uuid = info.uuid;
|
uuid = info.uuid;
|
||||||
str = info.str;
|
str = info.str;
|
||||||
o = info.o;
|
o = info.o;
|
||||||
|
10
util/util.h
10
util/util.h
@ -44,19 +44,23 @@ enum FrameType : int16_t {
|
|||||||
|
|
||||||
// 此结构体成员顺序不可变动,涉及到序列化反序列化。
|
// 此结构体成员顺序不可变动,涉及到序列化反序列化。
|
||||||
struct CMessageInfo {
|
struct CMessageInfo {
|
||||||
CMessageInfo() = default;
|
CMessageInfo(const std::string& id);
|
||||||
CMessageInfo(const CMessageInfo& info);
|
CMessageInfo(const CMessageInfo& info);
|
||||||
CMessageInfo& operator=(const CMessageInfo& info);
|
CMessageInfo& operator=(const CMessageInfo& info);
|
||||||
std::string cmd;
|
std::string id;
|
||||||
std::string uuid;
|
std::string uuid;
|
||||||
std::string str;
|
std::string str;
|
||||||
std::string o;
|
std::string o;
|
||||||
};
|
};
|
||||||
|
|
||||||
void serialize(const CMessageInfo& msg_info, char** out_buf, int& len);
|
void serialize(const CMessageInfo& msg_info, char** out_buf, int& len);
|
||||||
bool deserialize(const char* data, int len, CMessageInfo& msg_info);
|
bool deserialize(char* data, int len, CMessageInfo& msg_info);
|
||||||
std::string u8tolocal(const std::string& str);
|
std::string u8tolocal(const std::string& str);
|
||||||
std::string localtou8(const std::string& str);
|
std::string localtou8(const std::string& str);
|
||||||
|
void hash(const char* data, uint8_t k[32]);
|
||||||
|
void rdm(uint8_t* o, size_t size);
|
||||||
|
bool encrypt(const uint8_t* k, uint8_t* m, size_t len);
|
||||||
|
bool decrypt(const uint8_t* k, uint8_t* m, size_t len);
|
||||||
|
|
||||||
using namespace ofen;
|
using namespace ofen;
|
||||||
class CFrameBuffer
|
class CFrameBuffer
|
||||||
|
Loading…
x
Reference in New Issue
Block a user