rust-cid icon indicating copy to clipboard operation
rust-cid copied to clipboard

cidsum bin?

Open kallisti5 opened this issue 3 years ago • 6 comments

Given you need an IPFS server to generate checksums of files currently, it seems like a cidsum or ipldsum binary to quickly generate the CID hash for files would be a handy tool to provide from rust-cid.

kallisti5 avatar Dec 13 '20 15:12 kallisti5

Hm. While rust-cid looks to work as expected for something like this, it's missing the "Protobuf UnixFS" content. I'm assuming this is the "Codec"?

kallisti5 avatar Dec 13 '20 16:12 kallisti5

use cid::Cid;
use multihash::{Code, MultihashDigest};
use std::env;

fn main() {
        let args: Vec<String> = env::args().collect();
        if args.len() != 2 {
                println!("usage: cidsum <file>");
                std::process::exit(1);
        }

        let data = match std::fs::read(&args[1]) {
                Ok(d) => d,
                Err(x) => {
                        println!("error: {:?}", x);
                        std::process::exit(1);
                }
        };

        let h = Code::Sha2_256.digest(&data);
        let cid = Cid::new_v0(h).unwrap();

        let cid_string = cid.to_string();
        println!("{}", cid_string);
}

This obviously wouldn't work well with large files, it needs some chunking for the sha256 checksum.

kallisti5 avatar Dec 13 '20 16:12 kallisti5

Size of blocks is limited. You should check if the file size doesn't exceed 1mb. In addition you should use the raw codec 0x55 as this isn't protobuf.

dvc94ch avatar Dec 13 '20 17:12 dvc94ch

@kallisti5 Do I understand correctly that you would want to have a tool that gets the same CID as IPFS would produce?

If yes, that's out of scope for rust-cid. You would need a full UnixFSv1 implementation to determine the CID. Such a tool is closer related to IPFS implementations.

vmx avatar Dec 14 '20 11:12 vmx

Correct, that's the end goal. If protocol buffers are required, it indeed sounds like an external project would be more ideal.

kallisti5 avatar Dec 14 '20 15:12 kallisti5

It seems that https://gitlab.com/NukeManDan/rust_cid_npm aims to provide something like cidsum

eighthave avatar Aug 01 '22 16:08 eighthave