Include validatecid from new cid package
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Mar 1, 2018 at 21:06 UTC
903ecd1a7b336b0d8c7031bbc4e46746c73e36de
2 files changed
+121
removeme/verifcid/validate.go
new
+62
@@ -0,0 +1,62 @@
1
+package verifcid
2
+
3
+import (
4
+ "fmt"
5
+
6
+ mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
7
+ cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
8
+)
9
+
10
+var ErrPossiblyInsecureHashFunction = fmt.Errorf("potentially insecure hash functions not allowed")
11
+var ErrBelowMinimumHashLength = fmt.Errorf("hashes must be at least bytes long")
12
+
13
+const minimumHashLength = 20
14
+
15
+var goodset = map[uint64]bool{
16
+ mh.SHA2_256: true,
17
+ mh.SHA2_512: true,
18
+ mh.SHA3_224: true,
19
+ mh.SHA3_256: true,
20
+ mh.SHA3_384: true,
21
+ mh.SHA3_512: true,
22
+ mh.SHAKE_256: true,
23
+ mh.DBL_SHA2_256: true,
24
+ mh.KECCAK_224: true,
25
+ mh.KECCAK_256: true,
26
+ mh.KECCAK_384: true,
27
+ mh.KECCAK_512: true,
28
+ mh.ID: true,
29
+
30
+ mh.SHA1: true, // not really secure but still useful
31
+}
32
+
33
+func IsGoodHash(code uint64) bool {
34
+ good, found := goodset[code]
35
+ if good {
36
+ return true
37
+ }
38
+
39
+ if !found {
40
+ if code >= mh.BLAKE2B_MIN+19 && code <= mh.BLAKE2B_MAX {
41
+ return true
42
+ }
43
+ if code >= mh.BLAKE2S_MIN+19 && code <= mh.BLAKE2S_MAX {
44
+ return true
45
+ }
46
+ }
47
+
48
+ return false
49
+}
50
+
51
+func ValidateCid(c *cid.Cid) error {
52
+ pref := c.Prefix()
53
+ if !IsGoodHash(pref.MhType) {
54
+ return ErrPossiblyInsecureHashFunction
55
+ }
56
+
57
+ if pref.MhType != mh.ID && pref.MhLength < minimumHashLength {
58
+ return ErrBelowMinimumHashLength
59
+ }
60
+
61
+ return nil
62
+}
removeme/verifcid/validate_test.go
new
+59
@@ -0,0 +1,59 @@
1
+package verifcid
2
+
3
+import (
4
+ "testing"
5
+
6
+ mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
7
+
8
+ cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
9
+)
10
+
11
+func TestValidateCids(t *testing.T) {
12
+ assertTrue := func(v bool) {
13
+ t.Helper()
14
+ if !v {
15
+ t.Fatal("expected success")
16
+ }
17
+ }
18
+ assertFalse := func(v bool) {
19
+ t.Helper()
20
+ if v {
21
+ t.Fatal("expected failure")
22
+ }
23
+ }
24
+
25
+ assertTrue(IsGoodHash(mh.SHA2_256))
26
+ assertTrue(IsGoodHash(mh.BLAKE2B_MIN + 32))
27
+ assertTrue(IsGoodHash(mh.DBL_SHA2_256))
28
+ assertTrue(IsGoodHash(mh.KECCAK_256))
29
+ assertTrue(IsGoodHash(mh.SHA3))
30
+
31
+ assertTrue(IsGoodHash(mh.SHA1))
32
+
33
+ assertFalse(IsGoodHash(mh.BLAKE2B_MIN + 5))
34
+
35
+ mhcid := func(code uint64, length int) *cid.Cid {
36
+ mhash, err := mh.Sum([]byte{}, code, length)
37
+ if err != nil {
38
+ t.Fatal(err)
39
+ }
40
+ return cid.NewCidV1(cid.DagCBOR, mhash)
41
+ }
42
+
43
+ cases := []struct {
44
+ cid *cid.Cid
45
+ err error
46
+ }{
47
+ {mhcid(mh.SHA2_256, 32), nil},
48
+ {mhcid(mh.SHA2_256, 16), ErrBelowMinimumHashLength},
49
+ {mhcid(mh.MURMUR3, 4), ErrPossiblyInsecureHashFunction},
50
+ }
51
+
52
+ for i, cas := range cases {
53
+ if ValidateCid(cas.cid) != cas.err {
54
+ t.Errorf("wrong result in case of %s (index %d). Expected: %s, got %s",
55
+ cas.cid, i, cas.err, ValidateCid(cas.cid))
56
+ }
57
+ }
58
+
59
+}