如何在IPFS中重新创建多哈希的哈希摘要 [英] How recreate a hash digest of a multihash in IPFS

查看:132
本文介绍了如何在IPFS中重新创建多哈希的哈希摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要像这样向IPFS添加数据:

Assuming I'm adding data to IPFS like this:

$ echo Hello World | ipfs add

这将给我QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u-一个CID,它是Base58编码的Multihash.

This will give me QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u - a CID which is a Base58 encoded Multihash.

将其转换为Base16,告诉我IPFS添加的哈希摘要是SHA2-256哈希:

Converting it to Base16, tells me that the hash digest for what IPFS has added is a SHA2-256 hash:

12 - 20 - 74410577111096cd817a3faed78630f2245636beded412d3b212a2e09ba593ca
<hash-type> - <hash-length> - <hash-digest>

我知道IPFS不仅会散列数据,而且实际上会先将其序列化为Unixfs protobuf,然后再将其放入dag中.

I know that IPFS doesn't just hash the data, but actually serializes it as Unixfs protobuf first and then puts that in a dag.

我想揭开神秘面纱,如何获取74410577111096cd817a3faed78630f2245636beded412d3b212a2e09ba593ca,但我不确定如何获取创建的dag,该dag将Unixfs protobuf与数据一起保存.

I'd like to demystify, how to get to the 74410577111096cd817a3faed78630f2245636beded412d3b212a2e09ba593ca but I'm not really sure how to get hold of the created dag that holds the Unixfs protobuf with the data.

例如,我可以将序列化的原始数据写入磁盘并使用protobuf解码器进行检查:

For example I can write the serialized raw data to disk and inspect it with a protobuf decoder:

$ ipfs block get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u > /tmp/block.raw
$ protoc --decode_raw < /tmp/block.raw

这将为我提供可读格式的序列化数据:

This will give me the serialized data in a readable format:

1 {
  1: 2
  2: "Hello World\n"
  3: 12
}

但是,通过SHA-256传递给我的哈希仍然不同,这是有道理的,因为IPFS将protobuf置于dag中并对其进行多哈希处理.

However, piping that through SHA-256 still gives me a different hash, which makes sense because IPFS puts the protobuf in a dag and multihashes that one.

$ protoc --decode_raw < /tmp/block.raw | shasum -a 256

所以我决定弄清楚如何掌握该dag节点,自己对其进行散列以得到我要查找的散列.

So I decided to figure out how to get hold of that dag node, hash it myself to get to the hash I'm looking for.

我希望使用ipfs dag get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u可以给我一个多哈希,然后可以对其进行解码,但事实证明,它会返回一些我不知道如何检查的其他数据哈希:

I was hoping using ipfs dag get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u will give me a multihash that can then be decoded, but it turns out it returns some other data hash that I don't know how to inspect:

$ ipfs dag get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u
$ {"data":"CAISDEhlbGxvIFdvcmxkChgM","links":[]}

关于如何从此处解码data的任何想法?

Any ideas on how to decode data from here?

更新

data是原始数据的Base64表示形式: https://github .com/ipfs/go-ipfs/issues/4115

data is a Base64 representation of the original data: https://github.com/ipfs/go-ipfs/issues/4115

推荐答案

您要查找的哈希是ipfs block get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u输出的哈希. IPFS对编码值进行哈希处理.

The hash you're looking for is the hash of the output of ipfs block get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u. IPFS hashes the encoded value.

代替运行:

protoc --decode_raw < /tmp/block.raw | shasum -a 256

只需运行:

shasum -a 256 < /tmp/block.raw


但事实证明,它还会返回其他一些我不知道如何检查的数据哈希

but it turns out it returns some other data hash that I don't know how to inspect

那是因为我们当前在protobuf内部使用protobuf.外部protobuf具有结构{Data: DATA, Links: [{Name: ..., Size: ..., Hash: ...}]}.

That's because we currently use a protobuf inside of a protobuf. The outer protobuf has the structure {Data: DATA, Links: [{Name: ..., Size: ..., Hash: ...}]}.

在:

1 {
  1: 2
  2: "Hello World\n"
  3: 12
}

1 { ... }部分是外部probubuf的 Data 字段.但是,protoc --decode_raw *recursively* decodes this object so it decodes the Data`字段为:

The 1 { ... } part is the Data field of the outer protobuf. However, protoc --decode_raw *recursively* decodes this object so it decodes theData` field to:

  • 字段1(数据类型):2(文件)
  • 字段2(数据):"Hello World \ n"
  • 字段3(文件大小):12(字节)

对于上下文,相关的protobuf定义为:

For context, the relevant protobuf definitions are:

外部:

// An IPFS MerkleDAG Link
message PBLink {

  // multihash of the target object
  optional bytes Hash = 1;

  // utf string name. should be unique per object
  optional string Name = 2;

  // cumulative size of target object
  optional uint64 Tsize = 3;
}

// An IPFS MerkleDAG Node
message PBNode {

  // refs to other objects
  repeated PBLink Links = 2;

  // opaque user data
  optional bytes Data = 1;
}

内部:

message Data {
    enum DataType {
        Raw = 0;
        Directory = 1;
        File = 2;
        Metadata = 3;
        Symlink = 4;
        HAMTShard = 5;
    }

    required DataType Type = 1;
    optional bytes Data = 2;
    optional uint64 filesize = 3;
    repeated uint64 blocksizes = 4;

    optional uint64 hashType = 5;
    optional uint64 fanout = 6;
}

message Metadata {
    optional string MimeType = 1;
}

这篇关于如何在IPFS中重新创建多哈希的哈希摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆