Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions lib/devise/encryptor.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,50 @@
# frozen_string_literal: true

require 'bcrypt'
require 'digest'

module Devise
module Encryptor
def self.digest(klass, password)
if klass.pepper.present?
password = "#{password}#{klass.pepper}"
end
# This converts the password (of any length) into a fixed
# 64-character hex string, safely under the 72-char limit
password = Digest::SHA256.hexdigest(password)

# BCrypt the pre-hashed string
::BCrypt::Password.create(password, cost: klass.stretches).to_s
end

# Compares a potential password with a stored hash.
#
# It attempts the new (SHA-256 -> BCrypt) method first.
# If that fails, it falls back to the old (direct BCrypt) method
# to support existing passwords that were not pre-hashed
def self.compare(klass, hashed_password, password)
return false if hashed_password.blank?
bcrypt = ::BCrypt::Password.new(hashed_password)

begin
bcrypt = ::BCrypt::Password.new(hashed_password)
rescue ::BCrypt::Errors::InvalidHash
return false
end

if klass.pepper.present?
password = "#{password}#{klass.pepper}"
end
password = ::BCrypt::Engine.hash_secret(password, bcrypt.salt)
Devise.secure_compare(password, hashed_password)

# This is for passwords created with the new `digest` method.
pre_hashed_password = Digest::SHA256.hexdigest(password)
new_style_hash = ::BCrypt::Engine.hash_secret(pre_hashed_password, bcrypt.salt)

return true if Devise.secure_compare(new_style_hash, hashed_password)

# This is for passwords created before this change
# We re-run the original logic
old_style_hash = ::BCrypt::Engine.hash_secret(password, bcrypt.salt)
Devise.secure_compare(old_style_hash, hashed_password)
end
end
end
end
31 changes: 31 additions & 0 deletions test/encrytor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'test_helper'
require 'bcrypt'

class EncryptorTest < ActiveSupport::TestCase
test 'digest/compare passwords' do
hashed_password = Devise::Encryptor.digest(Devise, 'example')
assert Devise::Encryptor.compare(Devise, hashed_password, 'example')
assert_not Devise::Encryptor.compare(Devise, hashed_password, 'example1')
end

test 'false for incorrect bcrypt string' do
assert_not Devise::Encryptor.compare(Devise, 'incorrect_bcrypt_string', 'example')
end

test 'digest/compare support passwords longer 72 bytes' do
hashed_password = Devise::Encryptor.digest(Devise, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa123')
assert Devise::Encryptor.compare(Devise, hashed_password, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa123')
assert_not Devise::Encryptor.compare(Devise, hashed_password, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa125')
end

test 'digest/compare support old bcrypt only passwords' do
password = 'example'
password_with_pepper = "#{password}#{Devise.pepper}"
old_hashed_password =::BCrypt::Password.create(password_with_pepper, cost: Devise.stretches)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greptile 修正しなきゃいけない行ってここで合ってますか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、その通りです。26行目が修正が必要な行で合っています。

この行では、古いbcryptのみのパスワード(SHA256ハッシュを使わない従来の方式)をテストするために、直接BCrypt::Password.createを呼び出しています。

新しい実装では、Devise::Encryptorが内部でSHA256ハッシュを適用してからbcryptを使用するように変更されているので、古い形式のハッシュとの後方互換性をテストする必要があります。

この行は意図的に古い方式(SHA256を使わずに直接bcryptを適用)でパスワードハッシュを生成することで、既存のユーザーのパスワードが新しいシステムでも正しく検証できることを確認するためのテストケースです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greptile

test/encrytor_test.rb needs minor syntax correction on line 26 ってコメントくれたけど、どんな変更が必要?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

26行目の構文エラーは、::BCryptの前にスペースが不要なことです。

Suggested change
old_hashed_password =::BCrypt::Password.create(password_with_pepper, cost: Devise.stretches)
old_hashed_password = ::BCrypt::Password.create(password_with_pepper, cost: Devise.stretches)

=の後の不要なスペースを削除するだけで修正できます。


assert Devise::Encryptor.compare(Devise, old_hashed_password, password)
assert_not Devise::Encryptor.compare(Devise, old_hashed_password, 'examplo')
end
end