structhash icon indicating copy to clipboard operation
structhash copied to clipboard

Hash does not include type name

Open bradleyjkemp opened this issue 8 years ago • 1 comments

Currently the hash only includes the fields of a struct and not the name of the struct type. This causes potentially unwanted hash collisions as seen below:

type TypeA struct {
        Foo string
        Bar string
}

type TypeB struct {
        Foo string
        Bar string
}

func TestTags(t *testing.T) {
        t1 := TypeA{"foo", "bar"}
        t2 := TypeB{"foo", "bar"}
        h1, err := Hash(t1, 1)
        if err != nil {
                panic(err)
        }
        h2, err := Hash(t2, 1)
        if err != nil {
                panic(err)
        }
        if h1 == h2 {
                t.Error("hash collision")
        }
}

bradleyjkemp avatar Oct 05 '17 15:10 bradleyjkemp

Thanks for the report. I don't think i'll change the current behaviour, but I might add some extra calls to give you the ability to differentiate.

cnf avatar Oct 09 '17 16:10 cnf