我怎样才能散列旋风最小拦截密码? [英] How can I hash a password in Tornado with minimal blocking?

查看:183
本文介绍了我怎样才能散列旋风最小拦截密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PBKDF2 ,但是这也同样适用于BCrypt。

I'm using PBKDF2, but this applies equally to BCrypt.

一个合理的迭代次数可以很容易地0.5秒阻止散列密码。什么是借此出过程的轻量级的方式?我不愿意设置像芹菜或Gearman的只是此操作。

Hashing a password with a reasonable number of iterations can easily block for 0.5 seconds. What is a lightweight way to take this out of process? I'm reluctant to setup something like Celery or Gearman just for this operation.

推荐答案

您可以使用一个线程。这不会阻止龙卷风。你说你有一个散列密码的处理程序。那么这两个相关的方法可能是这样的:

You could use a thread. This will not block tornado. Say that you have a handler that hashes passwords. Then the two relevant methods might look like this:

import threading

def on_message(self, message):
    # pull out user and password from message somehow
    thread = threading.Thread(target=self.hash_password, args=(user, password))
    thread.start()


def hash_password(self, user, password):
    # do the hash and save to db or check against hashed password

您既可以等待线程在 ON_MESSAGE 方法完成,然后写响应,或者如果你不需要发送响应,那么就让它结束并保存在 hash_password 方法的结果。

You could either wait for the thread to finish in the on_message method then write the response, or if you don't need to send a response then just let it finish and save the results in the hash_password method.

如果你等待线程才能完成,你有你如何做到这一点要小心。 time.sleep 将阻塞,所以你会希望使用龙卷风非阻塞等待吧。

If you do wait for the thread to finish you have to be careful how you do it. time.sleep will block so you will want to use tornado's non blocking wait instead.

这篇关于我怎样才能散列旋风最小拦截密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆