mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 23:16:30 +01:00
Deduplicate digests from user configuration (#2522)
* deduplicate digests from user configuration Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * backout pointer reciever change on imageSource Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
@@ -22,6 +22,7 @@ func supportedHashAlgorithms() []crypto.Hash {
|
||||
}
|
||||
|
||||
func NewDigestsFromFile(closer io.ReadCloser, hashes []crypto.Hash) ([]file.Digest, error) {
|
||||
hashes = NormalizeHashes(hashes)
|
||||
// create a set of hasher objects tied together with a single writer to feed content into
|
||||
hashers := make([]hash.Hash, len(hashes))
|
||||
writers := make([]io.Writer, len(hashes))
|
||||
@@ -67,7 +68,7 @@ func Hashers(names ...string) ([]crypto.Hash, error) {
|
||||
}
|
||||
hashers = append(hashers, hashObj)
|
||||
}
|
||||
return hashers, nil
|
||||
return NormalizeHashes(hashers), nil
|
||||
}
|
||||
|
||||
func CleanDigestAlgorithmName(name string) string {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"sort"
|
||||
|
||||
"github.com/scylladb/go-set/uset"
|
||||
)
|
||||
|
||||
func NormalizeHashes(hashes []crypto.Hash) []crypto.Hash {
|
||||
set := uset.New()
|
||||
for _, h := range hashes {
|
||||
set.Add(uint(h))
|
||||
}
|
||||
list := set.List()
|
||||
sort.Slice(list, func(i, j int) bool {
|
||||
return list[i] < list[j]
|
||||
})
|
||||
result := make([]crypto.Hash, len(list))
|
||||
for i, v := range list {
|
||||
result[i] = crypto.Hash(v)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNormalizeHashes(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input []crypto.Hash
|
||||
want []crypto.Hash
|
||||
}{
|
||||
{
|
||||
name: "deduplicate hashes",
|
||||
input: []crypto.Hash{
|
||||
crypto.SHA1,
|
||||
crypto.SHA1,
|
||||
},
|
||||
want: []crypto.Hash{
|
||||
crypto.SHA1,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sort hashes",
|
||||
input: []crypto.Hash{
|
||||
crypto.SHA512,
|
||||
crypto.SHA1,
|
||||
},
|
||||
want: []crypto.Hash{
|
||||
crypto.SHA1,
|
||||
crypto.SHA512,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equal(t, tt.want, NormalizeHashes(tt.input))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user