我有一个包含几千条记录的用户身份验证表,其中包含一个由 bcrypt-ruby 加密的密码字段。我已将应用程序移植到 PHP/Yii 中,需要使用此字段进行身份验证。
有没有办法在 PHP 中检索这个 Ruby 创建的字段?
验证
我所说的“检索”是指我需要使用 PHP/Yii 应用程序对用户登录进行身份验证,以解释带有由 bcrypt-ruby 在 Rails 应用程序中创建的密码字段的数据库表。
最佳答案
我相信这会解决您的问题:
$database_record = "something"; // grab from database
$user_input = 'unicorns'; // take real one from post data
$password = crypt($user_input, '$2a$10$usesomesillystringforsalt$');
// key piece above is the second number, that is the 'work' factor
if (crypt($user_input, $database_record) == $password) {
echo "Password verified!";
}
else {
echo 'failed!'; }
这假设您使用 BCrypt::Password.create(desired_pass)
存储它们在 Ruby 中,并通过 BCrypt::Password.new(database_entry) == form_input
验证登录.
此外,要在您的数据库中创建新密码(即新用户),请存储
的结果 $password = crypt($user_input, '$2a$10$usesomesillystringforsalt$');
最后,确保您始终使用正确的成本系数。具有不同成本因子的相同密码将不等价。 bcrypt-ruby 中的默认成本因子是 10(当前版本,3.0.1)。
关于php - PHP 中的 Ruby bcrypt 密码检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9321075/