回答
不適用於字符串,但適用於[]byte
。見crypto/subtle
,尤其ConstantTimeCompare
:
func ConstantTimeCompare(x, y []byte) int
ConstantTimeCompare返回1當且僅當兩個相等長度的切片,x和y,具有相等的內容。所花費的時間是切片長度的函數,並且與內容無關。
正如你可能知道,你可以將字符串很容易轉換爲字節片:
var x []byte = []byte("someString")
使用'subtle.ConstantTimeEq'來比較切片的長度也很重要,因爲'subtle.ConstantTimeCompare'需要「兩個等長的切片」。它有一些「微妙」的行爲,否則。示例:http://play.golang.org/p/Xga-wsZvhT – Intermernet
在上面的[示例](http://play.golang.org/p/Xga-wsZvhT)中,行爲似乎是正確的。不等長的切片不相等。 – stevvooe
- 1. 定時安全地比較字符串
- 2. 在Go中比較字符串
- 3. Go如何做字符串比較?
- 4. 全局字符串比較
- 5. 字符串比較 - 安卓
- 6. 是在PHP字符串比較中的二進制安全的===?
- 7. 在Go中比較不等長字符串
- 8. 字符串比較C# - 全字匹配
- 9. 如何比較go模板中的字符串?
- 10. 字符/字符串比較
- 11. 字符串字符比較
- 12. 比較字符串
- 13. 比較字符串
- 14. 比較字符串
- 15. 字符串比較
- 16. 比較字符串
- 17. 字符串比較
- 18. 字符串比較
- 19. 字符串比較?
- 20. 字符串比較
- 21. 字符串比較
- 22. 字符串比較
- 23. 比較字符串
- 24. 比較字符串
- 25. 字符串比較
- 26. 字符串比較
- 27. 字符串比較
- 28. 字符串比較
- 29. 字符串比較
- 30. 字符串比較
secure_compare不是一個常數時間的方法,它調用each_byte,它遍歷每個字節的字符串中 http://apidock.com/ruby/String/each_byte –
「恆定時間」和「安全」是非常不同的目標;請澄清你到底想要什麼 – Vitruvius
@SethHoenig它是在定時攻擊的情況下。常量時間比較與時間複雜度無關,僅意味着比較函數在發現差異時不會早期返回(這會泄漏多少輸入不同的信息)。比較函數僅取決於輸入的長度,而不取決於內容。 – nemo