actpred: 代码审计(16项发现全部修复)+ 文档更新
审计报告: docs/reviews/actpred_CODE_AUDIT_20260808.md - P0(2项): episode索引错位(dataset.py)、推理归一化静默失败(predict.py) - P1(6项): 空训练集校验、图像自然排序、空样本防御、形状校验、 MobileNet下载错误提示、stats路径校验 - P2(8项): README示例修正、公共属性、glob优化、版本约束、 单元测试(17项)、GPU安装说明、BooleanOptionalAction、ONNX验证 新增文件: actpred/test_actpred.py (17项单元测试, 全部通过) 更新文档: README.md配置表扩充、INDEX.md、审计报告修复总结
This commit is contained in:
1 parent
1e34348d6c
commit
3d03f7a1ce
19 files changed
+850
-177
No files matched your search
+21
-19
@@ -1,12 +1,18 @@
|
||||
#pragma once
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
#include <iostream>
|
||||
#include <openssl/crypto.h>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include "Crypto_Basic.hpp"
|
||||
|
||||
struct EVP_MD_CTX_deleter_sha {
|
||||
void operator()(EVP_MD_CTX* ctx) const { EVP_MD_CTX_free(ctx); }
|
||||
};
|
||||
using EVP_MD_CTX_ptr_sha = std::unique_ptr<EVP_MD_CTX, EVP_MD_CTX_deleter_sha>;
|
||||
|
||||
namespace qing {
|
||||
|
||||
class SHA256: public qing::Crypto_Basic {
|
||||
@@ -40,14 +46,18 @@ namespace qing {
|
||||
|
||||
/* 验证数据是否与给定的哈希值匹配 */
|
||||
bool verify(const std::string& input_text, const std::string& expected_hash_hex) {
|
||||
std::string computed_hash = hash(input_text);
|
||||
return computed_hash == expected_hash_hex;
|
||||
std::vector<unsigned char> computed = hash_bytes(input_text);
|
||||
std::vector<unsigned char> expected = hex_to_bytes(expected_hash_hex);
|
||||
if (computed.size() != expected.size()) return false;
|
||||
return CRYPTO_memcmp(computed.data(), expected.data(), computed.size()) == 0;
|
||||
}
|
||||
|
||||
/* 验证数据是否与给定的哈希值匹配(字节向量版本) */
|
||||
bool verify(const std::vector<unsigned char>& input_bytes, const std::string& expected_hash_hex) {
|
||||
std::string computed_hash = hash(input_bytes);
|
||||
return computed_hash == expected_hash_hex;
|
||||
std::vector<unsigned char> computed = sha256_hash(input_bytes);
|
||||
std::vector<unsigned char> expected = hex_to_bytes(expected_hash_hex);
|
||||
if (computed.size() != expected.size()) return false;
|
||||
return CRYPTO_memcmp(computed.data(), expected.data(), computed.size()) == 0;
|
||||
}
|
||||
|
||||
/* 静态方法:直接计算SHA-256哈希(工具函数) */
|
||||
@@ -62,39 +72,31 @@ namespace qing {
|
||||
return hasher.hash_bytes(input_text);
|
||||
}
|
||||
|
||||
/* 打印哈希值(调试用) */
|
||||
static void print_hash(const std::vector<unsigned char>& hash_bytes) {
|
||||
std::string hex = bytes_to_hex(hash_bytes);
|
||||
std::cerr << hex << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
/* SHA-256哈希核心实现 */
|
||||
static std::vector<unsigned char> sha256_hash(const std::vector<unsigned char>& input) {
|
||||
// 创建EVP上下文
|
||||
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
|
||||
// 创建EVP上下文(RAII 自动释放)
|
||||
EVP_MD_CTX_ptr_sha ctx(EVP_MD_CTX_new());
|
||||
CRYPTO_CHECK(ctx != nullptr, "无法创建哈希上下文");
|
||||
|
||||
// 初始化SHA-256操作
|
||||
CRYPTO_CHECK(EVP_DigestInit_ex(ctx, EVP_sha256(), nullptr) == 1,
|
||||
CRYPTO_CHECK(EVP_DigestInit_ex(ctx.get(), EVP_sha256(), nullptr) == 1,
|
||||
"SHA-256初始化失败");
|
||||
|
||||
// 提供输入数据
|
||||
CRYPTO_CHECK(EVP_DigestUpdate(ctx, input.data(), input.size()) == 1,
|
||||
CRYPTO_CHECK(EVP_DigestUpdate(ctx.get(), input.data(), input.size()) == 1,
|
||||
"SHA-256更新失败");
|
||||
|
||||
// 获取哈希结果
|
||||
std::vector<unsigned char> digest(EVP_MD_CTX_size(ctx)); // SHA-256输出32字节
|
||||
std::vector<unsigned char> digest(EVP_MD_CTX_size(ctx.get())); // SHA-256输出32字节
|
||||
unsigned int digest_len = 0;
|
||||
CRYPTO_CHECK(EVP_DigestFinal_ex(ctx, digest.data(), &digest_len) == 1,
|
||||
CRYPTO_CHECK(EVP_DigestFinal_ex(ctx.get(), digest.data(), &digest_len) == 1,
|
||||
"SHA-256完成失败");
|
||||
|
||||
// 调整到实际长度(应该是32字节)
|
||||
digest.resize(digest_len);
|
||||
|
||||
// 清理上下文
|
||||
EVP_MD_CTX_free(ctx);
|
||||
|
||||
return digest;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user