Files
upstream-centrifugo/internal/tools/compare.go
upstream-mirror b352a9a30d
Some checks failed
test / Lint (push) Failing after 3s
test / Test with Go 1.26.8 (push) Failing after 2s
snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:17:21 +05:00

31 lines
1.0 KiB
Go

package tools
import "crypto/subtle"
// SecureCompare use constant time function to compare the two given array.
func SecureCompare(given, actual []byte) bool {
if subtle.ConstantTimeEq(int32(len(given)), int32(len(actual))) == 1 {
return subtle.ConstantTimeCompare(given, actual) == 1
}
// Securely compare actual to itself to keep constant time, but always return false
if subtle.ConstantTimeCompare(actual, actual) == 1 {
return false
}
return false
}
// SecureCompareString use constant time function to compare the two given string.
func SecureCompareString(given, actual string) bool {
// The following code is incorrect:
// return SecureCompare([]byte(given), []byte(actual))
if subtle.ConstantTimeEq(int32(len(given)), int32(len(actual))) == 1 {
return subtle.ConstantTimeCompare([]byte(given), []byte(actual)) == 1
}
// Securely compare actual to itself to keep constant time, but always return false
if subtle.ConstantTimeCompare([]byte(actual), []byte(actual)) == 1 {
return false
}
return false
}