MultiHash 算法 c++ 实现

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 

GitHub - lockblox/multihash: C++ Implementation of MultihashC++ Implementation of Multihash. Contribute to lockblox/multihash development by creating an account on GitHub.https://github.com/lockblox/multihash5. 步骤:

  • 拉取 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 .

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值