c++ - 如何在 C++ 中可移植地计算 sha1 哈希?

标签 c++ hash sha1

目标是计算 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/

相关文章:

c++ - QAudioOutput 代码是否存在内存泄漏?

c++ - 关于在 C/C++/Assembly 中返回多个值

c# - 在 C# 中实现 GetHashCode。空值处理

c++ - sha-1 c++ 中的堆栈检查失败

sha1 - 是否可以在 javacard 框架 2.2.1 版本中使用 'ALG_HMAC_SHA1' 类中的 'Signature' 方法?

android - 未经授权,安卓推送通知

c++ - 关于foreach循环和性能的几个问题

c++ - MFC header 中的语法错误

c++ - 如何将专用字符串映射到指定的整数

python - 生成可变长度哈希