Golang crypto/subtle.ConstantTimeCopy() function example

package crypto/subtle

ConstantTimeCopy copies the contents of y(3rd param) into x(2nd param) iff v == 1. If v == 0, x is left unchanged. Its behavior is undefined if v(1st param) takes any other value.

Golang crypto/subtle.ConstantTimeCopy() function usage example

 package main

 import (
 "crypto/subtle"
 "fmt"
 )

 func main() {
 y := []byte("Hello")
 x := make([]byte, len(y))

 subtle.ConstantTimeCopy(0, x, y) // v = 0
 fmt.Printf("%s\n", x) // will NOT copy y to x

 subtle.ConstantTimeCopy(1, x, y) // v = 1
 fmt.Printf("%s\n", x) // will copy y to x
 }

Output :

go run constanttimecopy.go

__

Hello

Reference :

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

Advertisement