Golang crypto/subtle.ConstantTimeSelect() function example

package crypto/subtle

ConstantTimeSelect returns x(2nd param) if v is 1 and y(3rd param) if v is 0. Its behavior is undefined if v(1st param) takes any other value.

Golang crypto/subtle.ConstantTimeSelect() function usage example

 package main

 import (
 "fmt"
 "crypto/subtle"
 )

 func main() {
 x := 3
 y := 4

 n := subtle.ConstantTimeSelect(1,x,y) // v = 1
 fmt.Printf("n : %d \n", n) // returns x


 nn := subtle.ConstantTimeSelect(0,x,y) // v = 0
 fmt.Printf("nn : %d \n", nn) // returns y

 }

Output :

n : 3

nn : 4

Reference :

http://golang.org/pkg/crypto/subtle/#ConstantTimeSelect

Advertisement