我不确定使用boost的UUID的那个是否会正确地在哈希值中执行前导零(你的字符串应该始终具有相同的长度),所以这里是上面例子的简化版本,它将做到这一点:
#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);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6039 次 |
最近记录: |