github.com/ericlagergren/ctb@v0.0.0-20220810041818-96749d9c394d/dudect/examples/cmp-good.go (about)

     1  // +build main
     2  
     3  package main
     4  
     5  import (
     6  	"crypto/rand"
     7  	"crypto/subtle"
     8  	"os"
     9  	"runtime"
    10  	"runtime/debug"
    11  	"time"
    12  
    13  	"github.com/elagergren/ctb/dudect"
    14  )
    15  
    16  func main() {
    17  	debug.SetGCPercent(-1)
    18  
    19  	runtime.LockOSThread()
    20  	defer runtime.UnlockOSThread()
    21  
    22  	const N = 512
    23  	cfg := &dudect.Config{
    24  		ChunkSize:    N,
    25  		Measurements: 1000,
    26  		Output:       os.Stderr,
    27  	}
    28  	ctx := dudect.NewContext(cfg)
    29  
    30  	secret := make([]byte, N)
    31  	_, err := rand.Read(secret)
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  	fn := func(data []byte) bool {
    36  		return subtle.ConstantTimeCompare(data, secret) == 1
    37  	}
    38  	t := time.NewTimer(10 * time.Second)
    39  	for {
    40  		select {
    41  		case <-t.C:
    42  			break
    43  		default:
    44  		}
    45  		if ctx.Test(fn, nil) {
    46  			break
    47  		}
    48  	}
    49  }