1. MultiHash 散列描述
Multihash is a protocol for differentiating outputs from various well-established hash functions, addressing size + encoding considerations. It is useful to write applications that future-proof their use of hashes, and allow multiple hash functions to coexist.
参考:https://multiformats.io/multihash/https://multiformats.io/multihash/
2. 散列格式
Multihash 遵循 TLV (type-length-value)模式。
- the type
<hash-func-type>
is an unsigned variable integer identifying the hash function. There is a default table, and it is configurable. The default table is the multicodec table. - the length
<digest-length>
is an unsigned variable integer counting the length of the digest, in bytes - the value
<digest-value>
is the hash function digest, with a length of exactly<digest-length>
bytes.
<hash-func-type>
<digest-length>
<digest-value>
正在使用的哈希函数的无符号变体代码 / unsigned varint digest length, in bytes / 散列函数输出值,其长度与前缀长度值相匹配
例子:
122041dd7b6443542e75701aa98a0c235951a28a0d851b11564d20022ab11d2589a8
12 ———— Hashing function encoded as varint: sha2-256 (code in hex:0x12)
20 ———— Length: 32 (in hex: 0x20)41dd7b6443542e75701aa98a0c235951a28a0d851b11564d20022ab11d2589a8 ———— Digest
3. MultiHash 实现
https://github.com/multiformats/multihashhttps://github.com/multiformats/multihash4. c++ 实现 MultiHash
- 拉取 cpp-multihash
git clone https://github.com/lockblox/multihash.git
- 安装依赖库
git clone https://github.com/lockblox/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat
.\vcpkg integrate install
- 因为项目要用到Crypto++的大整数功能,使用vcpkg快速集成。
- 在vcpkg install的时候,提示“VCPKG Warning: The following VS instances are excluded because the English language pack”,在VS2017中修改,勾选“英语”语言包。
- 安装gtest:
.\vcpkg install gtest:x64-windows
安装cryptopp ms-gsl:
.\vcpkg install cryptopp:x64-windows
.\vcpkg install ms-gsl:x64-windows
安装 varint :
.\vcpkg install varint:x64-windows
失败报错“CMake Error at scripts/cmake/vcpkg_download_distfile.cmake:99 (message):File does not have expected hash:”,处理方法:
修改“.\vcpkg\ports\varint\”目录下的文件portfile.cmake,用actual hash内容替换文件中的SHA512内容;
- 安装varint 结果:
- Building with CMake
cmake -DCMAKE_TOOLCHAIN_FILE=C:/Users/Administrator/Desktop/multihash-master/vcpkg/scripts/buildsystems/vcpkg.cmake C:/Users/Administrator/Desktop/multihash-master
cmake --build .
- 报错:
代码存在一些逻辑问题,疑似varint和multihash模板类的使用问题,调试无果,暂时放弃。
6. 更换代码分支
https://github.com/bonedaddy/multihash.githttps://github.com/bonedaddy/multihash.git配置方法和上边相同。
git clone https://github.com/lockblox/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat
.\vcpkg integrate install
.\vcpkg install gtest cryptopp ms-gsl varint
cd ..
cmake -DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake ./
cmake --build .
结果: