Skip to content

Commit cd11a73

Browse files
committed
postgres: add XXH64 vulnerability hash for comparison
Signed-off-by: Hank Donnay <hdonnay@redhat.com> Change-Id: I3f9b6a1675678dca3e669dced0007a8a6a6a6964
1 parent cb60e77 commit cd11a73

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

datastore/postgres/bench_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,25 @@ func BenchmarkMD5Vuln(b *testing.B) {
8484
b.Run("Baseline", run(md5Baseline))
8585
b.Run("Current", run(md5Vuln))
8686
}
87+
88+
func BenchmarkVulnerabilityHash(b *testing.B) {
89+
vs := test.GenUniqueVulnerabilities(1, "test")
90+
b.Run("MD5", func(b *testing.B) {
91+
b.ReportAllocs()
92+
for b.Loop() {
93+
kind, hash := md5Vuln(vs[0])
94+
if kind != hashMD5 || len(hash) != 16 {
95+
b.Fatal("???")
96+
}
97+
}
98+
})
99+
b.Run("XXH64", func(b *testing.B) {
100+
b.ReportAllocs()
101+
for b.Loop() {
102+
kind, hash := xxhVuln(vs[0])
103+
if kind != hashXXH64 || len(hash) != 8 {
104+
b.Fatal("???")
105+
}
106+
}
107+
})
108+
}

datastore/postgres/hash.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"sync"
88
"unsafe"
99

10+
"github.com/cespare/xxhash/v2"
11+
1012
"github.com/quay/claircore"
1113
"github.com/quay/claircore/internal/wart"
1214
)
@@ -65,10 +67,14 @@ func writeString(w io.Writer, s string) (int, error) {
6567
}
6668

6769
const (
68-
hashMD5 = `md5`
70+
hashMD5 = `md5`
71+
hashXXH64 = `xxh64`
6972
)
7073

71-
var md5Pool sync.Pool
74+
var (
75+
md5Pool sync.Pool
76+
xxhPool sync.Pool
77+
)
7278

7379
func getMD5() hash.Hash {
7480
if v := md5Pool.Get(); v != nil {
@@ -91,3 +97,22 @@ func md5Vuln(v *claircore.Vulnerability) (string, []byte) {
9197
doHash(h, v)
9298
return hashMD5, h.Sum(nil)
9399
}
100+
101+
func getXXH() *xxhash.Digest {
102+
if v := xxhPool.Get(); v != nil {
103+
return v.(*xxhash.Digest)
104+
}
105+
return xxhash.New()
106+
}
107+
108+
func putXXH(d *xxhash.Digest) {
109+
d.Reset()
110+
xxhPool.Put(d)
111+
}
112+
113+
func xxhVuln(v *claircore.Vulnerability) (string, []byte) {
114+
h := getXXH()
115+
defer putXXH(h)
116+
doHash(h, v)
117+
return hashXXH64, h.Sum(nil)
118+
}

0 commit comments

Comments
 (0)