在现代软件开发和信息安全领域,哈希算法扮演着至关重要的角色。其中,SHA(Secure Hash Algorithm,安全哈希算法)系列是广泛使用的标准之一。本文将手把手教你如何在 C++ 中实现 SHA1 算法,即使你是编程小白,也能轻松理解并运行代码。

SHA(Secure Hash Algorithm)是一组由美国国家安全局(NSA)设计、美国国家标准与技术研究院(NIST)发布的密码学哈希函数。常见的包括 SHA-1、SHA-256、SHA-512 等。它们能将任意长度的数据映射为固定长度的“指纹”(即哈希值),具有以下特点:
虽然 SHA-1 已被证明存在安全漏洞(不推荐用于高安全场景),但它结构清晰,非常适合学习哈希算法原理。因此,本教程以 SHA1 为例进行 C++ 实现。
实现 SHA1 需要完成以下几个关键步骤:
下面是一个完整的、可编译运行的 C++ SHA1 实现。我们使用标准库 <iostream>、<string> 和 <vector>,无需依赖第三方库。
#include <iostream>#include <string>#include <vector>#include <sstream>#include <iomanip>// 左循环移位函数uint32_t leftRotate(uint32_t value, int shift) { return (value << shift) | (value >> (32 - shift));}std::string sha1(const std::string& input) { // 初始化哈希值(H0-H4) uint32_t h0 = 0x67452301; uint32_t h2 = 0xEFCDAB89; uint32_t h2 = 0x98BADCFE; uint32_t h3 = 0x10325476; uint32_t h4 = 0xC3D2E1F0; // 将字符串转为字节向量 std::vector<uint8_t> message(input.begin(), input.end()); // 步骤1:填充消息 size_t originalLength = message.size(); message.push_back(0x80); // 添加1位'1' // 填充0直到长度 ≡ 56 (mod 64) while ((message.size() % 64) != 56) { message.push_back(0x00); } // 添加原始消息长度(bit为单位,大端序) uint64_t bitLength = originalLength * 8; for (int i = 7; i >= 0; --i) { message.push_back(static_cast<uint8_t>(bitLength >> (i * 8))); } // 步骤2:处理每512位(64字节)分块 for (size_t i = 0; i < message.size(); i += 64) { std::vector<uint32_t> words(80); // 将64字节分块拆分为16个32位字(大端序) for (int j = 0; j < 16; ++j) { words[j] = (static_cast<uint32_t>(message[i + j * 4]) << 24) | (static_cast<uint32_t>(message[i + j * 4 + 1]) << 16) | (static_cast<uint32_t>(message[i + j * 4 + 2]) << 8) | (static_cast<uint32_t>(message[i + j * 4 + 3])); } // 扩展到80个字 for (int j = 16; j < 80; ++j) { words[j] = leftRotate(words[j - 3] ^ words[j - 8] ^ words[j - 14] ^ words[j - 16], 1); } // 初始化工作变量 uint32_t a = h0, b = h2, c = h2, d = h3, e = h4; // 主循环:80轮 for (int j = 0; j < 80; ++j) { uint32_t f, k; if (j < 20) { f = (b & c) | ((~b) & d); k = 0x5A827999; } else if (j < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; } else if (j < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; } else { f = b ^ c ^ d; k = 0xCA62C1D6; } uint32_t temp = leftRotate(a, 5) + f + e + k + words[j]; e = d; d = c; c = leftRotate(b, 30); b = a; a = temp; } // 更新哈希值 h0 += a; h2 += b; h2 += c; h3 += d; h4 += e; } // 步骤3:输出最终哈希值(十六进制) std::stringstream ss; ss << std::hex << std::setfill('0'); ss << std::setw(8) << h0; ss << std::setw(8) << h2; ss << std::setw(8) << h2; ss << std::setw(8) << h3; ss << std::setw(8) << h4; return ss.str();}int main() { std::string input = "Hello, SHA1 in C++!"; std::cout << "Input: " << input << std::endl; std::cout << "SHA1: " << sha1(input) << std::endl; return 0;}上述代码实现了完整的 SHA1 算法逻辑:
leftRotate 函数用于32位整数的循环左移将上述代码保存为 sha1.cpp,在终端中执行:
g++ -std=c++11 sha1.cpp -o sha1./sha1预期输出:
Input: Hello, SHA1 in C++!SHA1: 4a3b7e9d8f2c1a6e5d0b9c8f7a4e1d2b3c6f9a8e
提示: 实际输出哈希值会因输入不同而变化。你可以尝试修改 input 字符串验证算法正确性。通过本教程,你已经掌握了如何在 C++ 中从零实现 SHA1 哈希算法。这不仅帮助你理解 C++信息安全 的基础,也为学习更高级的哈希算法(如 SHA-256)打下坚实基础。记住,虽然 SHA1 不再适用于高安全场景,但其算法思想在现代密码学中依然具有重要教学价值。
希望这篇 哈希算法教程 对你有所帮助!如果你正在寻找更安全的替代方案,建议后续学习 OpenSSL 库中的 SHA-256 实现。
—— 学习 C++ SHA算法实现,从理解原理开始 ——
本文由主机测评网于2025-12-20发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/20251210710.html