我一直在使用现有的散列算法来散列使用基本身份验证进行身份验证的小型微服务的密码。
根据社区标准,我选择 bcrypt 算法来散列密码。但是在使用 Apache Benchmark 对服务器进行基准测试后,我发现 90% 的 CPU 周期都用于验证密码。给出一个上下文,一个 t3.large 能够在没有身份验证的情况下处理 60 个请求/秒,而在使用身份验证逻辑的情况下只能处理 6 个请求/秒。
我想过使用 python 中可用的 passlib 库进行基准测试,这里是使用 passlib 和默认设置进行 100 次迭代的结果 -
print (timeit.timeit('my_ctx.verify("password", hash_sha256)', setup=setup, number=100))
40.74972726893611
print (timeit.timeit('my_ctx.verify("password", hash_md5)', setup=setup, number=100))
0.03434068092610687
print (timeit.timeit('my_ctx.verify("password", hash_des)', setup=setup, number=100))
0.01271090202499181
print (timeit.timeit('my_ctx.verify("password", hash_bcrypt)', setup=setup, number=100))
25.593560334993526
print (timeit.timeit('my_ctx.verify("password", hash_sha512)', setup=setup, number=100))
46.78381339798216
print (timeit.timeit('my_ctx.verify("password", hash_pbkdf2)', setup=setup, number=100))
2.236785114975646
print (timeit.timeit('my_ctx.verify("password", hash_argon2)', setup=setup, number=100))
12.668332702014595
我知道在幕后进行了多轮讨论。将 sha256 的轮数更改为 1000 后,出现了显着差异 -
timeit.timeit('sha256_crypt.hash("password", rounds=1000)', setup=setup, number=100)
所以我的问题是 -
对于使用基本身份验证而不是基于令牌的身份验证或任何其他方式的小型微服务,我想问一下最佳方法是什么,以便密码安全地驻留在托管在亚马逊服务器上的多级安全数据库中?