PHP 是不是也太“任性”了点…… 关于 bcrypt

2015-09-04 09:27:51 +08:00
 cevincheung

password_hash第二个参数PASSWORD_BCRYPTPASSWORD_DEFAULT分明就是同一个参数……

还有。 bcrypt 计算出来的为啥是$2y 开头的? python 中 bcrypt 计算出来的都是$2a 开头的啊,为啥? php 生成的密码到 python 里还能用么?

4504 次点击
所在节点    问与答
17 条回复
haiyang416
2015-09-04 09:41:39 +08:00
直接用估计是不行的了, PHP 的 Base64 字符集都和标准的不同。
haiyang416
2015-09-04 10:00:01 +08:00
@haiyang416 先收回这句话,搜了一圈没找到出处了。
caixiexin
2015-09-04 10:00:09 +08:00
模块化算法 BCrypt 的格式由

$2$ , $2a$ or $2y$ 标识的哈希算法和格式。
两个数字参数表示的成本值, 后面跟着 $
一个字符长的 53 base-64-encoded ( 它们使用字母值 . , / , 0 – 9 , A – Z , a – z 不同的 标准 Base 64 Encoding 字母组成的) :
仅 22 字符的盐( 有效地 128 ) 132 解码后的位位
31 加密的字符输出( 有效地只 184 ) 186 解码后的位位
因此总长度是 59 或者 60 字节分别。
-----------------------
以前查 bcrypt 资料看到的,可能 python 和 php 实现 bcrypt 哈希算法略有不同。具体你可以找找 bcrypt 的详细说明:)
haiyang416
2015-09-04 10:06:38 +08:00
breeswish
2015-09-04 12:20:09 +08:00
固定 cost 试试
cevincheung
2015-09-04 12:45:44 +08:00
@breeswish 跟 cost 没关系, cost 是第二部分的值比如$2y$cost$、$2y$07$
cevincheung
2015-09-04 12:47:28 +08:00
@caixiexin 所以这个算法不是统一的么?是不是可以理解成 php 的 bcrypt 未“完全”实现?
skydiver
2015-09-04 12:50:00 +08:00
CRYPT_BLOWFISH - Blowfish hashing with a salt as follows: "$2a$", "$2x$" or "$2y$", a two digit cost parameter, "$", and 22 characters from the alphabet "./0-9A-Za-z". Using characters outside of this range in the salt will cause crypt () to return a zero-length string. The two digit cost parameter is the base-2 logarithm of the iteration count for the underlying Blowfish-based hashing algorithmeter and must be in range 04-31, values outside this range will cause crypt () to fail. Versions of PHP before 5.3.7 only support "$2a$" as the salt prefix: PHP 5.3.7 introduced the new prefixes to fix a security weakness in the Blowfish implementation. Please refer to » this document for full details of the security fix, but to summarise, developers targeting only PHP 5.3.7 and later should use "$2y$" in preference to "$2a$".

这里有说明 http://php.net/manual/en/function.crypt.php
cevincheung
2015-09-04 12:51:55 +08:00
@skydiver 所以潜台词是$2a 可能存在不安全的潜在风险?
breeswish
2015-09-04 12:52:40 +08:00
@cevincheung 抱歉没仔细看 :-) 确实是算法不一样,具体解释在这里

http://security.stackexchange.com/questions/20541/insecure-versions-of-crypt-hashes/20543#20543

Python 下可以用 passlib.hash.bcrypt 生成 2y 的 bcrypt : https://pythonhosted.org/passlib/lib/passlib.hash.bcrypt.html
skydiver
2015-09-04 12:52:49 +08:00
@cevincheung 看着是这个意思
laoyuan
2015-09-04 16:27:35 +08:00
毕竟宇宙第一
raincious
2015-09-05 12:17:22 +08:00
关于 2a 和 2y 前缀的问题,其实这个跟你用的 Salt 有关系,如果你的 Salt 是 2a 开头的,那么 crypt 就会用 2a 算法运算结果然后得到一个 2a 开头的序列, 2y 也同理。

password_hash 默认的 Salt 给的就是 2y 前缀,所以自然结果也就是 2y 开头的。
cevincheung
2015-09-05 15:11:44 +08:00
@raincious

这样的
var_dump (PASSWORD_DEFAULT )结果是 integer 1
var_dump (PASSWORD_BCRYPT )结果是 integer 1

那给这两个配置有毛用?
raincious
2015-09-05 15:32:07 +08:00
@raincious

你可以理解成 PASSWORD_DEFAULT 就是选择一个默认值,而默认值是 PASSWORD_BCRYPT 啊。( *手动笑哭*

要是有多个常数的话,可能就不会有这种感觉了,

比如选项是:
PASSWORD_ASMILEPT
PASSWORD_BCRYPT
PASSWORD_CLAUGHPT
PASSWORD_DRAGEPT
PASSWORD_ENVYPT
PASSWORD_FROWNPT
PASSWORD_GIGGLEPT

默认值 PASSWORD_DEFAULT 是 PASSWORD_BCRYPT
chaegumi
2016-08-11 14:29:12 +08:00
最搞笑的是现在要接入 jasig cas ,别人是 2a php 是 2y ,接入 jasig cas 的密码验证还得自己写个 2y 的来匹配
verytoex
2017-12-21 16:08:36 +08:00
我测试过,用 spring.security 来校验 PHP 创建的 hash,只要把 2y 替换成 2a 就能校验通过了。
PHP 兼容 2a 和 2y,两个都支持。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/218183

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX