Skip to content

Commit

Permalink
replace byHash helper
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Ramlot <[email protected]>
  • Loading branch information
inteon committed Jul 14, 2024
1 parent 64e38ee commit a61c915
Showing 1 changed file with 4 additions and 20 deletions.
24 changes: 4 additions & 20 deletions pkg/bundle/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"encoding/hex"
"encoding/pem"
"fmt"
"sort"
"slices"
"strings"

jks "github.com/pavlo-v-chernykh/keystore-go/v4"
Expand Down Expand Up @@ -344,24 +344,6 @@ func (b *bundleData) populateData(bundles []string, formats *trustapi.Additional
return nil
}

// byHash is a type alias for sorting the slice of [32]byte arrays
type byHash [][32]byte

// Len returns the length of the slice
func (a byHash) Len() int {
return len(a)
}

// Swap swaps two elements in the slice
func (a byHash) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}

// Less compares two elements in the slice
func (a byHash) Less(i, j int) bool {
return bytes.Compare(a[i][:], a[j][:]) == -1
}

// remove duplicate certificates from bundles and sort certificates by hash
func deduplicateAndSortBundles(bundles []string) ([]string, error) {
var block *pem.Block
Expand Down Expand Up @@ -395,7 +377,9 @@ func deduplicateAndSortBundles(bundles []string) ([]string, error) {
for key := range certificatesHashes {
orderedKeys = append(orderedKeys, key)
}
sort.Sort(byHash(orderedKeys))
slices.SortFunc(orderedKeys, func(a, b [32]byte) int {
return bytes.Compare(a[:], b[:])
})

var sortedDeduplicatedCerts []string
for _, key := range orderedKeys {
Expand Down

0 comments on commit a61c915

Please sign in to comment.