Sha256: 45898ef0e205c50a6cd02ad2fba345339683d0658da005038a5548780646befb
Contents?: true
Size: 702 Bytes
Versions: 16
Compression:
Stored size: 702 Bytes
Contents
package crypto import "fmt" // ConstantTimeByteCompare is a constant-time byte comparison of x and y. This function performs an absolute comparison // if the two byte slices assuming they represent a big-endian number. // // error if len(x) != len(y) // -1 if x < y // 0 if x == y // +1 if x > y func ConstantTimeByteCompare(x, y []byte) (int, error) { if len(x) != len(y) { return 0, fmt.Errorf("slice lengths do not match") } xLarger, yLarger := 0, 0 for i := 0; i < len(x); i++ { xByte, yByte := int(x[i]), int(y[i]) x := ((yByte - xByte) >> 8) & 1 y := ((xByte - yByte) >> 8) & 1 xLarger |= x &^ yLarger yLarger |= y &^ xLarger } return xLarger - yLarger, nil }
Version data entries
16 entries across 16 versions & 1 rubygems