加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用bcrypt-ruby使用版本$2y验证散列密码

发布时间:2020-12-13 22:06:05 所属栏目:PHP教程 来源:网络整理
导读:我们需要使用 Ruby来针对现有的用户数据库授权用户.用户的密码都是使用password_compat PHP库生成的.所有哈希密码都以$2y开头. 我一直在使用bcrypt-ruby来尝试验证用户,但我没有找到任何成功. #This user's password is "password"irb(main):041:0 g = BCryp
我们需要使用 Ruby来针对现有的用户数据库授权用户.用户的密码都是使用password_compat PHP库生成的.所有哈希密码都以$2y开头.

我一直在使用bcrypt-ruby来尝试验证用户,但我没有找到任何成功.

#This user's password is "password"
irb(main):041:0> g = BCrypt::Password.new("$2y$10$jD.PlMQwFSYSdu4imy8oCOdqKFq/FDlW./x9cMxoUmcLgdvKCDNd6")
=> "$2y$10$jD.PlMQwFSYSdu4imy8oCOdqKFq/FDlW./x9cMxoUmcLgdvKCDNd6"
irb(main):042:0> g == "password"
=> false
irb(main):044:0> g.version
=> "2y"
irb(main):045:0> g.cost
=> 10
irb(main):046:0> g.salt
=> "$2y$10$jD.PlMQwFSYSdu4imy8oCO"
irb(main):047:0> g.hash
=> -219334950017117414

我对bcrypt或加密一般都不是很熟悉. bcrypt-ruby可以处理$2y吗?我查看了源代码,但我认为不可以.这是底层操作系统的故障(我使用OS X)?

解决方法

是的,bcrypt-ruby可以使用2y处理密码哈希.您只需要用2a替换2y:

irb(main):002:0> BCrypt::Password.new("$2a$10$jD.PlMQwFSYSdu4imy8oCOdqKFq/FDlW./x9cMxoUmcLgdvKCDNd6") == "password"
=> true

这是必要的,因为bcrypt-ruby似乎遵循Solar Designer’s first suggestion to introduce just 2x for a backward-compatible support for the “sign extension bug”:

[…] I am considering keeping support
for the broken hashes under another prefix – say,“$2x$” (where the “x”
would stand for “sign eXtension bug”) instead of the usual “$2a$”.

he proposed to also introduce the 2y prefix年晚些时候,为了更好地区分三个版本:

One idea is to allocate yet another prefix,which will mean the same
thing as 2a
,but “certified” as passing a certain specific test suite
(which will include 8-bit chars). So we’ll have:

2a – unknown correctness (may be correct,may be buggy)
2x – sign extension bug
2y – definitely correct

Newly set/changed passwords will be getting the new prefix.

PHP supports 2a,2x,and 2y而bcrypt-ruby supports only 2a,and 2x.但是如果你知道你的实现没有“符号扩展错误”,你可以用2a替换2y,因为2y意味着与2a相同.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读