4

我有一个包含数千条记录的用户身份验证表,其中包含由 bcrypt-ruby 加密的密码字段。我已将应用程序移植到 PHP / Yii 中,并且需要使用此字段进行身份验证。

有没有办法在 PHP 中检索这个 Ruby 创建的字段?

确认

通过“检索”,我的意思是我需要使用 PHP / Yii 应用程序对用户登录进行身份验证,以解释带有 bcrypt-ruby 在 Rails 应用程序中创建的密码字段的数据库表。

4

2 回答 2

4

我相信这会解决你的问题:

$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)。

于 2012-02-17T05:21:27.343 回答
0

你应该看看PHP.net上的 crypt 函数

如果您在 Ruby 中正确地遵循了 bcrypt,那么您应该可以在这里实现您想要的。

于 2012-02-17T01:45:19.223 回答