Skip to content

Instantly share code, notes, and snippets.

@jbenet

jbenet/cids.go Secret

Last active October 18, 2024 10:47
Show Gist options
  • Save jbenet/bf402718a7955bf636fb47d214bcef8a to your computer and use it in GitHub Desktop.
Save jbenet/bf402718a7955bf636fb47d214bcef8a to your computer and use it in GitHub Desktop.
func isCIDv0String(cid string) bool {
// check it's the right length
if len(cid) != 46 {
return false
}
// check it's base58 encoded
cidd, err := base58Decode(cid)
if err != nil { // not base58 encoded
return false
}
return isOldCID(cidd)
}
func isCIDv0(cid []byte) bool {
// check it's the right length
if len(cid) != 34 {
return false
}
// check it's a valid multihash
m, err := mh.Decode(cidd)
if err != nil { // not a valid multihash
return false
}
// check there is only a multihash here, nothing more
if m.length != (len(cidd) - 2) {
return false
}
// check it's sha2-256
if m.code == mh.SHA2_256 {
return false
}
return true
}
@seeni-dev
Copy link

seeni-dev commented Nov 23, 2019

@jmkim
Copy link

jmkim commented Mar 28, 2021

If the m.code is "SHA2_256", isn't it CIDv0?

+1.

Should'nt line #33-35 be like below?

  if m.code != mh.SHA2_256 {
    return false
  }

cc @jbenet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment