如何创建一个 laravel 哈希密码
我正在尝试为 Laravel 创建一个散列密码.现在有人告诉我使用 Laravel 哈希助手,但我似乎找不到它,或者我看错了方向.
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 哈希密码?在哪里?
How do I create a laravel hashed password? And where?
我知道代码是什么,但我不知道在哪里以及如何使用它,所以它给了我散列密码.如果我得到哈希密码,那么我可以手动将其插入数据库
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
推荐答案
在 Laravel
中使用 Bcrypt 哈希密码:
Hashing A Password Using Bcrypt in Laravel
:
$password = Hash::make('yourpassword');
这将创建一个散列密码.您可以在您的控制器甚至模型中使用它,例如,如果用户使用表单使用 POST
方法向您的控制器提交密码,那么您可以使用以下方法对其进行哈希处理:
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 POST
method then you may hash it using something like this:
$password = Input::get('passwordformfield'); // password is form field
$hashed = Hash::make($password);
这里,$hashed
将包含散列密码.基本上,您将在创建/注册新用户时执行此操作,例如,如果用户提交详细信息,例如 name
、email
、username
和 password
等使用表单,然后在将数据插入数据库之前,您将在验证数据后对密码进行哈希处理.如需更多信息,请阅读文档.
Here, $hashed
will 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
, username
and password
etc 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.
更新:
$password = 'JohnDoe';
$hashedPassword = Hash::make($password);
echo $hashedPassword; // $2y$10$jSAr/RwmjhwioDlJErOk9OQEO7huLz9O6Iuf/udyGbHPiTNuB3Iuy
因此,您需要将 $hashedPassword
插入数据库.希望,现在很清楚,如果您仍然感到困惑,那么我建议您阅读一些教程,在 laracasts.com 和 tutsplus.com 并且还阅读了一本关于 Laravel
的书,这是一本免费的电子书,你可以下载.
So, you'll insert the $hashedPassword
into 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.com and tutsplus.com and also read a book on Laravel
, this is a free ebook, you may download it.
更新: 由于 OP
想要使用 Laravel Hash
手动加密密码,没有任何类或形式,所以这是使用 artisan tinker
来自命令提示符:
Update: Since OP
wants to manually encrypt password using Laravel Hash
without any class or form so this is an alternative way using artisan tinker
from command prompt:
- 转到您的命令提示符/终端
- 导航到
Laravel
安装(项目的根目录) - 使用
cd <directory name>
并从命令提示符/终端按回车 - 然后写
php artisan tinker
并回车 - 然后写
echo Hash::make('somestring');
- 您将在控制台上获得一个散列密码,复制它,然后做任何您想做的事情.
- Go to your command prompt/terminal
- Navigate to the
Laravel
installation (your project's root directory) - Use
cd <directory name>
and press enter from command prompt/terminal - Then write
php artisan tinker
and press enter - Then write
echo Hash::make('somestring');
- You'll get a hashed password on the console, copy it and then do whatever you want to do.
更新(Laravel 5.x):
// Also one can use bcrypt
$password = bcrypt('JohnDoe');
相关文章