gevent和强哈希密码方法

2024-06-02 07:59:11 发布

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

我使用的是gevent greenlet,所以我很受CPU计算的限制。我需要使用一个强大的散列方法来存储密码。你知道吗

当我不在gevent上下文中时,我有使用bcrypt的习惯,但我做了一个小测试:

import bcrypt
import time

password = b"toto"

start_hash  = time.clock()

hashed = bcrypt.hashpw(password, bcrypt.gensalt())

print 'time hash bcrypt %s' % (time.clock() - start_hash)

start_compare = time.clock()

assert bcrypt.hashpw(password, hashed) == hashed

elapsed = (time.clock() - start_compare)

print 'time check bcrypt %s' % elapsed

结果是:

time hash bcrypt 0.291887
time check bcrypt 0.293343

这花了太多的时间,就像在果岭里一样。你知道吗

作为比较,使用旧md5哈希的同一类型计算:

time hash md5 4.1e-05
time check hash md5 1.1e-05

我有什么办法?你知道吗


Tags: importtimecheckgeventpasswordhashstartmd5
1条回答
网友
1楼 · 发布于 2024-06-02 07:59:11

Gevent可以很好地与利用并发的网络和IO绑定函数一起工作,但是bcrypt没有这个功能。你知道吗

尝试使用Processlet和ObjectPool。Processlet关注CPU绑定的任务,比如哈希,而不是IO绑定的任务。在Processlet和ObjectPool中使用bcrypt的一个很好的例子是here。你知道吗

相关问题 更多 >