1. 常见的散列函数
foo (原文)
md5 128 bits 长度:32(不计前缀)
.......................................................................
acbd18db4cc2f85cedef654fccc4a4d8
.......................................................................
对输入仍以512位分组,其输出是4个32位字的级联
sha1 160 bits 长度:40(不计前缀)
.......................................................................
(1114)0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
.......................................................................
对长度小于264的输入,产生长度为160bit的散列值
sha2-256 256 bits 长度:64(不计前缀)
.......................................................................
(1220)2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
.......................................................................
可以将任意长度的消息,都会产生一个256位的哈希值
sha2-512(aka sha512) 512 bits 长度:128(不计前缀)
.......................................................................
(1340)f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7
.......................................................................
2. multihash算法使用
参考:
https://github.com/bonedaddy/multihashhttps://github.com/bonedaddy/multihash新建工程,设置包含目录:
.\multihash
.\vcpkg\installed\x86-windows\include
主函数xxx.cpp:
#include <iostream>
#include <sstream>
#include <digest.h>
#include <function.h>
#include <code.h>
#include <algorithm.h>
#include <digest.h>
#include <function.h>
template <typename Container>
std::ostream& operator<<(std::ostream& os,
const multihash::digest<Container>& hash) {
auto data = std::string_view(hash.data(), hash.size());
for (auto c : data) {
auto uc = uint8_t(c);
os << std::hex << std::setfill('0') << std::setw(2);
os << static_cast<int>(uc);
}
return os;
}
template <typename Container>
std::string toHexString(const multihash::digest<Container>& hash) {
std::ostringstream os;
os << hash;
return os.str();
}
int main()
{
std::string input("foo");
std::istringstream input_stream(input);
auto hash = multihash::function(multihash::code::sha1)(input.begin(),
input.end());
auto result = toHexString(hash);
std::cout << result << std::endl;
return 1;
}
为VS编译std添加C++17标准库:
项目属性,c/c++,语言,c++语言标准,选择“ISO C++17 标准 (/std:c++17)”
sha1算法运行结果: