检查 Moodle 的散列密码

Check Moodle's hashed password

Moodle 以这种格式在用户 table 中保存哈希密码:

If the stored password is:

y$UB6vKrpw227eqVXj2PiPou9c0eRtxsdU02fo9.wc3VtsA2FI.gS6a
then:
y$ = the id of the hashing algorithm used (crypt_blowfish), enclosed in dollar signs.
10$ = the cost of using that algorithm (two digits) followed by a dollar sign.
UB6vKrpw227eqVXj2PiPou = randomly generated secure salt (22 characters).
9c0eRtxsdU02fo9.wc3VtsA2FI.gS6a = the hash (31 characters).

我有明文密码。我不知道如何用 Python.

检查它

只需使用 bcrypt:

pip install bcrypt

那么调用checkpw()函数就可以了:

import bcrypt

hashed = b'y$UB6vKrpw227eqVXj2PiPou9c0eRtxsdU02fo9.wc3VtsA2FI.gS6a'

password = input('Enter password:').encode()
if bcrypt.checkpw(password, hashed):
    print('Correct password entered!')
else:
    print('Password is wrong!')

请注意,bcrypt 使用字节而不是字符串,这就是为什么用户输入必须是 运行 到 .encode()