我运行的Flask应用程序具有Flask-Security。我的config.py
文件如下所示:
...
# Flask-Security config
SECURITY_URL_PREFIX = "..." # my URL prefix
SECURITY_PASSWORD_HASH = "pbkdf2_sha512"
SECURITY_PASSWORD_SALT = "..." # my 29-character long salt
...
并在数据库上生成如下所示的哈希:
$pbkdf2-sha512$25000$pXROaU2JUSrlnDPm3BsjBA$ckspsls2SWPhl9dY7XDiAZh5yucWq27fWRuVj4aOUc5dA2Ez5VH1LYiz5KjaZJscaJYAFhWIwPhkAsHiPOrvrg
这是123456
哈希密码。
在另一个应用程序中,我需要验证此哈希,但是通过使用Flask-Security,它要求我位于具有相同配置集的Flask应用程序中。
我已经尝试了很多事情,但是没有Flask-Security我无法验证此密码,并且必须有一种方法可以做到这一点。
你们能帮我吗?
好的,感谢@jwag以及对Flask-Security源代码的更多探索,我已经成功做到了。
对于那些处于相同情况的人,这是我的工作:
import hmac
import hashlib
import base64
from passlib.context import CryptContext
text_type = str
salt = "YOUR_SALT_HERE"
def get_pw_context ():
pw_hash = 'pbkdf2_sha512'
schemes = ['bcrypt', 'des_crypt', 'pbkdf2_sha256', 'pbkdf2_sha512', 'sha256_crypt', 'sha512_crypt', 'plaintext']
deprecated = ['auto']
return CryptContext (schemes=schemes, default=pw_hash, deprecated=deprecated)
def encode_string(string):
if isinstance(string, text_type):
string = string.encode('utf-8')
return string
def get_hmac (password):
h = hmac.new(encode_string(salt), encode_string(password), hashlib.sha512)
return base64.b64encode(h.digest())
def verify_password (password, hash):
return get_pw_context().verify(get_hmac(password), hash)
# Finally
verify_password ("MY_PASSWORD", "MY_PASSWORD_HASH")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句