Make sure we have a SHA2_256, length 32 hash when creating a CidV0.

This commit is contained in:
Kevin Atkinson
2018-09-10 05:10:23 -04:00
parent 46dd393ad1
commit dfc48d3ec4

9
cid.go
View File

@@ -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)}
}