python passlib:“rounds”的最佳值是什么

2024-09-29 00:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

passlib documentation

For most public facing services, you can generally have signin take upwards of 250ms - 400ms before users start getting annoyed.

那么在登录/注册中rounds的最佳值是什么,如果我们考虑到对数据库的一个调用用于登录尝试,并且它使用MongoDB非阻塞调用。(使用Mongotor,并使用电子邮件作为_id,因此默认情况下是索引的,查询速度很快:0.0029978256226,当然,使用一个包含3条记录的数据库进行测试…)

import passlib.hash
import time

hashh = passlib.hash.pbkdf2_sha512
beg1 = time.time()
password = hashh.encrypt("test", salt_size = 32, rounds = 12000)
print time.time()- beg1 # returns 0.142999887466
beg2 = time.time()
hashh.verify("test", password) # returns 0.143000125885
print time.time()- beg2

现在如果我使用半值:

^{pr2}$

am使用Dell XPS 15 i7 2.0 Ghz上的64位Windows 7

注意:安装了bcrypt,当然,直接使用它作为默认值(rounds = 12)确实很麻烦:

hashh = passlib.hash.bcrypt
beg1 = time.time()
password = hashh.encrypt("test", rounds = 12) # returns 0.406000137329
print time.time()- beg1
beg2 = time.time()
hashh.verify("test", password) # returns 0.40499997139
print time.time()- beg2

半值:

password = hashh.encrypt("test", rounds = 12) # 0.00699996948242 wonderful?
hashh.verify("test", password) # 0.00600004196167

当使用pbkdf2_sha512时,你能给我一个好的舍入值吗?在


Tags: testimport数据库timepasswordhashreturnsencrypt
1条回答
网友
1楼 · 发布于 2024-09-29 00:20:50

(此处为passlib开发人员)

pbkdf2_sha512所用的时间与它的rounds参数(elapsed_time = rounds * native_speed)成线性比例。使用系统的数据,native_speed = 12000 / .143 = 83916 iterations/second,这意味着您需要大约83916 * .350 = 29575 rounds来获得~350ms的延迟。在

对于bcrypt来说,事情有点棘手,因为它所花费的时间与它的rounds参数(elapsed_time = (2 ** rounds) * native_speed)成对数比例。使用系统的数据,native_speed = (2 ** 12) / .405 = 10113 iterations/second,这意味着您需要大约log(10113 * .350, 2) = 11.79 rounds来获得~350毫秒的延迟。但是由于BCrypt只接受整数取整参数,所以您需要选择rounds=11(~200ms)或{}(~400ms)。在


所有这些都是我希望在passlib的未来版本中修复的。作为一个正在进行的工作,passlib的mercurial repo当前包含一个简单的小脚本choose_rounds.py,它负责为给定的目标时间选择正确的轮数值。您可以直接下载并运行它,如下所示(运行可能需要20秒左右):

$ python choose_rounds.py -h
usage: python choose_rounds.py <hash_name> [<target_in_milliseconds>]

$ python choose_rounds.py pbkdf2_sha512 350
hash............: pbkdf2_sha512
speed...........: 83916 iterations/second
target time.....: 350 ms
target rounds...: 29575  

$ python choose_rounds.py bcrypt 350
hash............: bcrypt
speed...........: 10113 iterations/second
target time.....: 350 ms
target rounds...: 11 (200ms   150ms faster than requested)
target rounds...: 12 (400ms   50ms slower than requested)

(编辑:添加了关于安全最小轮数的响应…

免责声明:确定一个安全的最小值是一个令人惊讶的棘手问题-有许多难以量化的参数,很少的真实世界数据,以及一些非常无用的理论。由于缺乏权威,我自己一直在研究这个主题;对于即兴计算,我将原始数据简化为一个简短的公式(如下),这是我通常使用的。只需注意,它背后是几页假设和粗略估计,这使得它更像是一个Fermi Estimation,而不是一个确切的答案:|

我使用GPU攻击PBKDF2-HMAC-SHA512的经验法则是:

^{pr2}$
  • days是攻击者猜测密码的50/50概率之前的天数。在
  • dollars是攻击者的硬件预算(美元)。在
  • n是用户密码中的平均熵量(以位为单位)。在

回答你的脚本孩子的问题:如果一个平均密码有32位的熵,并且攻击者有一个价值2000美元的系统和一个好的GPU,那么在30000轮时,他们需要30天(2**(32-31)*30000/2000)有50/50的几率破解给定的哈希。我建议你在轮数/天数之间进行权衡,直到你能接受为止。在

请记住:

  • 字典攻击的成功率不是线性的,它更多的是“长尾”情况,所以把50/50标记看作是半衰期。

  • 这是一个关键因素,因为它编码了使用特定技术水平攻击特定算法的成本估计。实际值2**-31衡量攻击者将付出的“每轮美元天数”。相比之下,使用ASIC攻击PBKDF2-HMAC-SHA512有一个更接近46更大的数字意味着攻击者的成本更大,每轮的安全性更低,尽管脚本小子通常不会有这种预算:)

相关问题 更多 >