目标是计算 SHA1作为 C++ 程序的一部分的缓冲区或多个缓冲区的散列。
最佳答案
我不确定使用 boost 的 UUID 的人是否会正确地在哈希值中做前导零(你的字符串应该始终具有相同的长度 afaik),所以这是上面示例的简化版本,它会这样做:
#include <cstdio>
#include <string>
#include <boost/uuid/sha1.hpp>
std::string get_sha1(const std::string& p_arg)
{
boost::uuids::detail::sha1 sha1;
sha1.process_bytes(p_arg.data(), p_arg.size());
unsigned hash[5] = {0};
sha1.get_digest(hash);
// Back to string
char buf[41] = {0};
for (int i = 0; i < 5; i++)
{
std::sprintf(buf + (i << 3), "%08x", hash[i]);
}
return std::string(buf);
}
关于c++ - 如何在 C++ 中可移植地计算 sha1 哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28489153/