rust-cid
rust-cid copied to clipboard
cidsum bin?
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.
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"?
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.
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.
@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.
Correct, that's the end goal. If protocol buffers are required, it indeed sounds like an external project would be more ideal.
It seems that https://gitlab.com/NukeManDan/rust_cid_npm aims to provide something like cidsum