Golang crypto/subtle.ConstantTimeCompare() function example
package crypto/subtle
ConstantTimeCompare returns 1 iff the two equal length slices, x(1st param) and y(2nd param), have equal contents. The time taken is a function of the length of the slices and is independent of the contents.
Golang crypto/subtle.ConstantTimeCompare() function usage example
package main
import (
"crypto/subtle"
"fmt"
)
func main() {
X := []byte("Hello")
Y := []byte("World")
Z := []byte("Hello")
n := subtle.ConstantTimeCompare(X,Y) // X != Y, n = 0
fmt.Printf("n : %d\n", n)
nn := subtle.ConstantTimeCompare(X,Z) // X == Z, nn = 1
fmt.Printf("nn : %d\n", nn)
}
Output :
n : 0
nn : 1
Reference :
Advertisement
Something interesting
Tutorials
+5.8k Unix/Linux : How to test user agents blocked successfully ?
+13.7k Golang : Check if an integer is negative or positive
+20.9k Golang : Convert PNG transparent background image to JPG or JPEG image
+10.9k Golang : Sieve of Eratosthenes algorithm
+6.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+12.9k Python : Convert IPv6 address to decimal and back to IPv6
+7.8k Golang : Example of how to detect which type of script a word belongs to
+11k Golang : Create S3 bucket with official aws-sdk-go package
+9.4k Golang : Apply Histogram Equalization to color images
+11.7k Golang : Calculations using complex numbers example
+7.9k Golang : Grayscale Image
+48.5k Golang : Upload file from web browser to server