Standard library / crypto/subtle

crypto/subtle.ConstantTimeCompare()

Compare equal-length slices without content-dependent early exit; differing lengths return 0 immediately. Functional tests are not timing or side-channel audits. Expected stdout: '1 0\n'.

Formal Syntax & Structure

go syntaxgo
func ConstantTimeCompare(x, y []byte) int

Parameters & Attributes

NameTypeDescription
x[]byteFirst operand.
y[]byteSecond operand.

Return Value

int

1 for true and 0 for false.

Working Example

Example Snippetgo
package main
import (  "crypto/subtle"  "fmt")
func main() {  fmt.Println(subtle.ConstantTimeCompare([]byte("Go"), []byte("Go")), subtle.ConstantTimeCompare([]byte("Go"), []byte("No")))}
Go Reference