什么是"&="运算符?为什么Twilio在比较字符串时会使用它?

Mic*_*ael 7 python operators twilio

什么是&=python?

例如:

for c1, c2 in izip(string1, string2):
    result &= c1 == c2
Run Code Online (Sandbox Code Playgroud)

我在twilio python库中找到它:https: //github.com/twilio/twilio-python/blob/master/twilio/util.py#L62

为什么他们不直接return string1 == string2比较字符串并比较每个字符?

use*_*740 12

请参阅secure_compare doctring:

比较两个字符串,同时防止时间攻击

通过强制评估每个角色,攻击者无法使用时间来猜测差异发生的位置 - 使用在第一个差异上立即返回的"正常"实现,这是可能的.

语义计数器result &= c1 == c2(当它们全部相同时成功)实际上是return c1 != c2(第一个区别的失败/中止),而不是提出的条件问题.

现在,result &= c1 == c2是一样的result = result & (c1 == c2),其中&(也称为bitwsie-AND)是一个严格的逻辑 - 并且超过布尔.这意味着当且仅当结果先前为True并且同情也为True 时,结果累加器的使用将保持为True.

  • 定时攻击几乎与线程安全正交. (2认同)