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

编程入门 行业动态 更新时间:2024-10-26 20:21:44
本文介绍了如何在C ++中可移植地计算sha1哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目标是计算一个缓冲区或多个缓冲区的 SHA1 哈希一个C ++程序.

The objective is to compute the SHA1 hash of a buffer or multiple buffers as part of a C++ program.

推荐答案

我不确定使用boost的UUID的那个是否会正确地将哈希值的前导零(您的字符串应始终具有相同的长度afaik),所以这里是上面示例的简化版本可以做到这一点:

I'm not sure whether the one using boost's UUID will do leading zeros in hash values correctly (your string should always have the same length afaik), so here's a simplified version of the example above which will do that:

#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 ++中可移植地计算sha1哈希?

本文发布于:2023-11-30 10:15:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   哈希

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!