diff --git a/pkg/crypto/ciphersuite/ciphersuite.go b/pkg/crypto/ciphersuite/ciphersuite.go index 87a31027d..4f2d83f3f 100644 --- a/pkg/crypto/ciphersuite/ciphersuite.go +++ b/pkg/crypto/ciphersuite/ciphersuite.go @@ -82,11 +82,9 @@ func examinePadding(payload []byte) (toRemove int, good byte) { good = byte(int32(^t) >> 31) //nolint:gosec //G115 // The maximum possible padding length plus the actual length field - toCheck := 256 - // The length of the padded data is public, so we can use an if here - if toCheck > len(payload) { - toCheck = len(payload) - } + toCheck := min( + // The length of the padded data is public, so we can use an if here + 256, len(payload)) for i := 0; i < toCheck; i++ { t := uint(paddingLen) - uint(i) //nolint:gosec //G115 diff --git a/pkg/crypto/fingerprint/fingerprint.go b/pkg/crypto/fingerprint/fingerprint.go index 43d4a4000..1df06ef69 100644 --- a/pkg/crypto/fingerprint/fingerprint.go +++ b/pkg/crypto/fingerprint/fingerprint.go @@ -28,7 +28,7 @@ func Fingerprint(cert *x509.Certificate, algo crypto.Hash) (string, error) { // https://golang.org/pkg/hash/#Hash i += n } - digest := []byte(fmt.Sprintf("%x", h.Sum(nil))) + digest := fmt.Appendf(nil, "%x", h.Sum(nil)) digestlen := len(digest) if digestlen == 0 { diff --git a/pkg/protocol/extension/alpn.go b/pkg/protocol/extension/alpn.go index 719428601..d6bb7eeaa 100644 --- a/pkg/protocol/extension/alpn.go +++ b/pkg/protocol/extension/alpn.go @@ -4,6 +4,8 @@ package extension import ( + "slices" + "golang.org/x/crypto/cryptobyte" ) @@ -72,10 +74,8 @@ func ALPNProtocolSelection(supportedProtocols, peerSupportedProtocols []string) return "", nil } for _, s := range supportedProtocols { - for _, c := range peerSupportedProtocols { - if s == c { - return s, nil - } + if slices.Contains(peerSupportedProtocols, s) { + return s, nil } } diff --git a/util.go b/util.go index 3d9b0bc85..3943ea8d2 100644 --- a/util.go +++ b/util.go @@ -3,12 +3,12 @@ package dtls +import "slices" + func findMatchingSRTPProfile(a, b []SRTPProtectionProfile) (SRTPProtectionProfile, bool) { for _, aProfile := range a { - for _, bProfile := range b { - if aProfile == bProfile { - return aProfile, true - } + if slices.Contains(b, aProfile) { + return aProfile, true } } @@ -31,10 +31,7 @@ func splitBytes(bytes []byte, splitLen int) [][]byte { splitBytes := make([][]byte, 0) numBytes := len(bytes) for i := 0; i < numBytes; i += splitLen { - j := i + splitLen - if j > numBytes { - j = numBytes - } + j := min(i+splitLen, numBytes) splitBytes = append(splitBytes, bytes[i:j]) }