Constant-time comparison

Constant-time comparison checks whether two secret values are equal without making the execution time depend on the position of the first differing byte. It helps prevent timing attacks that could reveal information about authentication tags or other secret values. Go provides hmac.Equal and subtle.ConstantTimeCompare for this purpose.

Minimal example

go
valid := hmac.Equal(providedTag, expectedTag)
if !valid {
	return ErrInvalid
}

Learn this concept

Lesson 2.21 · HMAC signing for cursors

Used in exercises

Exercise 2.22 · GET /v1/restaurants: signed cursors Exercise 8.4 · Payment processing service Exercise 8.13 · Timestamped webhook signature package
Prerequisite conceptsHMAC