From dfc48d3ec4812d9b0203edd8f0b517f2273604e0 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Mon, 10 Sep 2018 05:10:23 -0400 Subject: [PATCH] Make sure we have a SHA2_256, length 32 hash when creating a CidV0. --- cid.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cid.go b/cid.go index ececb26..cfcbcde 100644 --- a/cid.go +++ b/cid.go @@ -133,6 +133,15 @@ var CodecToStr = map[uint64]string{ // compatibility with the plain-multihash format used used in IPFS. // NewCidV1 should be used preferentially. func NewCidV0(mhash mh.Multihash) Cid { + // Need to make sure hash is valid for CidV0 otherwise we will + // incorrectly detect it as CidV1 in the Version() method + dec, err := mh.Decode(mhash) + if err != nil { + panic(err) + } + if dec.Code != mh.SHA2_256 || dec.Length != 32 { + panic("invalid hash for cidv0") + } return Cid{string(mhash)} }