是一种在Go中进行恒定时间字符串比较的内置方法吗?
Devise.secure_compare
当我在Ruby中需要这个功能时,我已经使用过这个方法.
不是用于字符串而是用于[]byte
.crypto/subtle
特别参见ConstantTimeCompare
:
func ConstantTimeCompare(x, y []byte) int
如果两个相等长度的切片x和y具有相同的内容,则ConstantTimeCompare返回1.所花费的时间是切片长度的函数,并且与内容无关.
您可能知道,您可以轻松地将字符串转换为字节切片:
var x []byte = []byte("someString")