Help us understand the problem. What is going on with this article?

PHPのpassword_hashで生成したBCryptハッシュがRubyで検証できずにハマった

More than 1 year has passed since last update.

起きたこと

  • 既存のCakePHP3.5+PHP7.2のアプリケーションではハッシュ化にBCryptを使用している
$passwordHash = password_hash('test', PASSWORD_DEFAULT);
echo $passwordHash
// => "$2y$10$i3J7OqQt9h33I5bwSwAge.H6qQtO2LZASwu36AJi3oC69qdXFSx5i"
  • 連携するRuby on Rails5.1+ruby2.5のアプリケーションでも同様にBCryptを採用した
password_hash = Crypt::Password.create("test")
p password_hash
# => "$2a$10$8vSfRpFpQYMXdYCDgV6i2.4yTOD.99DWqA/WYk8h5/YuOptzYJGUy"
  • PHPでは検証のためsaltからハッシュを再生成できるが、Ruby側では行えなかった
# PHPで作ったやつを検証したい
password_hash = "$2y$10$i3J7OqQt9h33I5bwSwAge.H6qQtO2LZASwu36AJi3oC69qdXFSx5i"
input_password_hash = BCrypt::Engine.hash_secret("test", BCrypt::Password.new(password_hash).salt)
p password_hash == input_password_hash
# => trueになるはずがfalse
p input_password_hash
# => なぜかnil

原因

解決方法

password_hash = "$2y$10$i3J7OqQt9h33I5bwSwAge.H6qQtO2LZASwu36AJi3oC69qdXFSx5i"
passsword_hash = password_hash.sub(/^.../, '$2a')
p BCrypt::Password.new(passsword_hash).is_password?("test")
# => true

これで良いんだろうか...というもやもやが残る...

良い解決方法があったらコメントで教えていただけると助かります。

っていうかバージョン違うなら例外くらい投げてくれてもいいのになんでnilが返されるんですかね!?!?

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした