2024-04-25 04:58:31 发布
网友
当我多次运行werkzeug.security.generate_password_hash("Same password")(docs)时,每次的输出都不同。
werkzeug.security.generate_password_hash("Same password")
我做错什么了?为什么它不是常数?
密码是salted,是的。在散列之前将salt添加到密码中,以确保散列在rainbow table attack中不可用。
因为salt是在每次调用函数时随机生成的,所以生成的密码散列也不同。返回的散列包含生成的salt,因此仍然可以正确验证密码。
演示:
>>> from werkzeug.security import generate_password_hash >>> generate_password_hash('foobar') 'pbkdf2:sha1:1000$tYqN0VeL$2ee2568465fa30c1e6680196f8bb9eb0d2ca072d' >>> generate_password_hash('foobar') 'pbkdf2:sha1:1000$XHj5nlLU$bb9a81bc54e7d6e11d9ab212cd143e768ea6225d'
这两个字符串不同;但是包含足够的信息来验证密码,因为生成的salt包含在每个字符串中:
# pbkdf2:sha1:1000$tYqN0VeL$2ee2568465fa30c1e6680196f8bb9eb0d2ca072d ^^^^^^^^^^^^^^^^ salt ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ algo info ^^^^^^^^ actual hash of the password (PBKDF2 applied SHA1 1000 times)
因为随机盐是tYqN0VeL对于one和XHj5nlLU,所以得到的散列也不同。
tYqN0VeL
XHj5nlLU
仍然可以根据以下任一哈希验证foobar密码:
foobar
>>> from werkzeug.security import check_password_hash >>> check_password_hash('pbkdf2:sha1:1000$tYqN0VeL$2ee2568465fa30c1e6680196f8bb9eb0d2ca072d', 'foobar') True >>> check_password_hash('pbkdf2:sha1:1000$XHj5nlLU$bb9a81bc54e7d6e11d9ab212cd143e768ea6225d', 'foobar') True
另见
密码是salted,是的。在散列之前将salt添加到密码中,以确保散列在rainbow table attack中不可用。
因为salt是在每次调用函数时随机生成的,所以生成的密码散列也不同。返回的散列包含生成的salt,因此仍然可以正确验证密码。
演示:
这两个字符串不同;但是包含足够的信息来验证密码,因为生成的salt包含在每个字符串中:
因为随机盐是
tYqN0VeL
对于one和XHj5nlLU
,所以得到的散列也不同。仍然可以根据以下任一哈希验证
foobar
密码:另见
相关问题 更多 >
编程相关推荐