php 如何创建 Laravel 哈希密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22846897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:19:11  来源:igfitidea点击:

How to create a laravel hashed password

phpsecuritylaravelhashpasswords

提问by Graham

I am trying to create an hashed password for Laravel. Now someone told me to use Laravel hash helper but I can't seem to find it or I'm looking in the wrong direction.

我正在尝试为 Laravel 创建一个散列密码。现在有人告诉我使用 Laravel 哈希助手,但我似乎找不到它,或者我看错了方向。

How do I create a laravel hashed password? And where?

如何创建 Laravel 哈希密码?在哪里?

Edit: I know what the code is but I don't know where and how to use it so it gives me back the hashed password. If I get the hashed password then I can manually insert it into the database

编辑:我知道代码是什么,但我不知道在哪里以及如何使用它,所以它给了我散列密码。如果我得到散列密码,那么我可以手动将其插入到数据库中

回答by The Alpha

Hashing A Password Using Bcrypt in Laravel:

使用 Bcrypt 散列密码Laravel

$password = Hash::make('yourpassword');

This will create a hashed password. You may use it in your controller or even in a model, for example, if a user submits a password using a form to your controller using POSTmethod then you may hash it using something like this:

这将创建一个散列密码。您可以在您的控制器甚至模型中使用它,例如,如果用户使用表单将密码提交给您的控制器 usingPOST方法,那么您可以使用如下方式散列它:

$password = Input::get('passwordformfield'); // password is form field
$hashed = Hash::make($password);

Here, $hashedwill contain the hashed password. Basically, you'll do it when creating/registering a new user, so, for example, if a user submits details such as, name, email, usernameand passwordetc using a form, then before you insert the data into database, you'll hash the password after validating the data. For more information, read the documentation.

在这里,$hashed将包含散列密码。基本上,你会创建/注册新用户,因此,举例来说做到这一点,如果用户提交的细节,例如,nameemailusernamepassword等使用的形式,那么你之前将数据插入到数据库中,你会散列验证数据后输入密码。有关更多信息,请阅读文档

Update:

更新:

$password = 'JohnDoe';
$hashedPassword = Hash::make($password);
echo $hashedPassword; // y$jSAr/RwmjhwioDlJErOk9OQEO7huLz9O6Iuf/udyGbHPiTNuB3Iuy

So, you'll insert the $hashedPasswordinto database. Hope, it's clear now and if still you are confused then i suggest you to read some tutorials, watch some screen casts on laracasts.comand tutsplus.comand also read a book on Laravel, this is a free ebook, you may download it.

因此,您将插入$hashedPassword到数据库中。希望,现在已经很清楚了,如果您仍然感到困惑,那么我建议您阅读一些教程,在laracasts.comtutsplus.com上观看一些屏幕投射并阅读一本书Laravel这是一本免费的电子书,您可以下载它。

Update:Since OPwants to manually encrypt password using Laravel Hashwithout any class or form so this is an alternative way using artisan tinkerfrom command prompt:

更新:由于OP想要使用 Laravel 手动加密密码Hash而无需任何类或表单,因此这是artisan tinker从命令提示符使用的另一种方法:

  1. Go to your command prompt/terminal
  2. Navigate to the Laravelinstallation (your project's root directory)
  3. Use cd <directory name>and press enter from command prompt/terminal
  4. Then write php artisan tinkerand press enter
  5. Then write echo Hash::make('somestring');
  6. You'll get a hashed password on the console, copy it and then do whatever you want to do.
  1. 转到您的命令提示符/终端
  2. 导航到Laravel安装(您项目的根目录)
  3. cd <directory name>从命令提示符/终端使用并按回车键
  4. 然后写入php artisan tinker并按回车
  5. 然后写 echo Hash::make('somestring');
  6. 你会在控制台上得到一个散列密码,复制它然后做任何你想做的事情。

Update (Laravel 5.x):

更新(Laravel 5.x):

// Also one can use bcrypt
$password = bcrypt('JohnDoe');


回答by Rao

Laravel 5 uses bcrypt. So, you can do this as well.

Laravel 5 使用bcrypt. 所以,你也可以这样做。

$hashedpassword = bcrypt('plaintextpassword');

output of which you can save to your database table's password field.

您可以将其输出保存到数据库表的密码字段中。

Fn Ref: bcrypt

Fn 参考:bcrypt

回答by Prashant Barve

The Laravel Hash facade provides secure Bcrypt hashing for storing user passwords.

Laravel Hash 门面为存储用户密码提供了安全的 Bcrypt 哈希。

Basic usage required two things:

基本用法需要两件事:

First include the Facade in your file

首先在您的文件中包含 Facade

use Illuminate\Support\Facades\Hash;

and use MakeMethod to generate password.

并使用MakeMethod 生成密码。

$hashedPassword = Hash::make($request->newPassword);

and when you want to match the Hashed string you can use the below code:

当您想匹配哈希字符串时,您可以使用以下代码:

Hash::check($request->newPasswordAtLogin, $hashedPassword)

You can learn more with the Laravel document link below for Hashing: https://laravel.com/docs/5.5/hashing

你可以通过下面的 Laravel 文档链接了解更多关于哈希的信息:https://laravel.com/docs/5.5/hashing

回答by Somnath Muluk

To store password in database, make hash of password and then save.

要将密码存储在数据库中,请对密码进行散列,然后保存。

$password = Input::get('password_from_user'); 
$hashed = Hash::make($password); // save $hashed value

To verify password, get password stored of account from database

要验证密码,请从数据库中获取存储的帐户密码

// $user is database object
// $inputs is Input from user
if( \Illuminate\Support\Facades\Hash::check( $inputs['password'], $user['password']) == false) {
  // Password is not matching 
} else {
  // Password is matching 
}

回答by Jathin Prasad

If you want to understand how excatly laravel works you can review the complete class on Github: https://github.com/illuminate/hashing/blob/master/BcryptHasher.php

如果您想了解 laravel 的工作原理,您可以查看 Github 上的完整课程:https: //github.com/illuminate/hashing/blob/master/BcryptHasher.php

But basically there are Three PHP methods involved on that:

但基本上涉及到三个 PHP 方法:

$pasword = 'user-password';
// To create a valid password out of laravel Try out!
$cost=10; // Default cost
$password = password_hash($pasword, PASSWORD_BCRYPT, ['cost' => $cost]);

// To validate the password you can use
$hash = 'y$NhRNj6QF.Bo6ePSRsClYD.4zHFyoQr/WOdcESjIuRsluN1DvzqSHm';

if (password_verify($pasword, $hash)) {
   echo 'Password is valid!';
} else {
   echo 'Invalid password.';
}

//Finally if you have a $hash but you want to know the information about that hash. 
print_r( password_get_info( $password_hash ));

The hashed password is same as laravel 5.x bcrypt password. No need to give salt and cost, it will take its default values.

散列密码与 laravel 5.x bcrypt 密码相同。无需提供盐和成本,它将采用其默认值。

Those methods has been implemented in the laravel class, but if you want to learn more please review the official documentation: http://php.net/manual/en/function.password-hash.php

这些方法已经在laravel类中实现了,但是如果你想了解更多请查看官方文档:http: //php.net/manual/en/function.password-hash.php

回答by Chris G

You can use the following:

您可以使用以下内容:

$hashed_password = Hash::make('Your Unhashed Password');

You can find more information: here

您可以找到更多信息:这里

回答by hendra1

In the BcryptHasher.php you can find the hash code:

在 BcryptHasher.php 中,您可以找到哈希代码:

public function make($value, array $options = array())
{
    $cost = isset($options['rounds']) ? $options['rounds'] : $this->rounds;

    $hash = password_hash($value, PASSWORD_BCRYPT, array('cost' => $cost));

            $hash = password_hash($value, PASSWORD_BCRYPT, array('cost' => $cost));
            echo $value.' '.PASSWORD_BCRYPT.' '.$cost.' ';
            echo $hash;die();
    if ($hash === false)
    {
        throw new RuntimeException("Bcrypt hashing not supported.");
    }

    return $hash;
}

回答by Dharmendra Patel

use Illuminate\Support\Facades\Hash;
if(Hash::check($plain-text,$hashed-text))
    {
       return true;
    }
    else
    {
        return false;
    }

eg- $plain-text = 'text'; $hashed-text=Hash::make('text');

eg- $plain-text = 'text'; $hashed-text=Hash::make('text');

回答by RashedRahat

Here is the solution:

这是解决方案:

use Illuminate\Support\Facades\Hash;    
$password = request('password'); // get the value of password field
$hashed = Hash::make($password); // encrypt the password

N.B: Use 1st line code at the very beginning in your controller. Last but not the least, use the rest two lines of code inside the function of your controller where you want to manipulate with data after the from is submitted. Happy coding :)

注意:在控制器的最开始使用第一行代码。最后但并非最不重要的一点是,在提交 from 后要在其中操作数据的控制器函数中使用其余两行代码。快乐编码:)

回答by Kamlesh

Compare password in laravel and lumen:

比较 Laravel 和 lumen 中的密码:

This may be possible that bcrypt function does not work with php7 then you can use below code in laravel and lumen as per your requirements:

这可能是 bcrypt 函数不适用于 php7,然后您可以根据您的要求在 laravel 和 lumen 中使用以下代码:

use Illuminate\Support\Facades\Hash;

$test = app('hash')->make("test");
if (Hash::check('test', $test)) {
   echo "matched";
} else {
   echo "no matched";
}

I hope, this help will make you happy :)

我希望,这种帮助会让你开心:)