如何创建 Laravel 哈希密码 [英] How to create a laravel hashed password

查看:28
本文介绍了如何创建 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 将包含散列的密码.基本上,您将在创建/注册新用户时执行此操作,例如,如果用户提交诸如 nameemailusername 之类的详细信息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.comtutsplus.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:

  1. 转到您的命令提示符/终端
  2. 导航到 Laravel 安装(您项目的根目录)
  3. 使用 cd <目录名称> 并从命令提示符/终端按 Enter
  4. 然后写php artisan tinker并回车
  5. 然后写echo Hash::make('somestring');
  6. 您将在控制台上获得一个经过哈希处理的密码,复制它,然后做任何您想做的事情.
  1. Go to your command prompt/terminal
  2. Navigate to the Laravel installation (your project's root directory)
  3. Use cd <directory name> and press enter from command prompt/terminal
  4. Then write php artisan tinker and 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.

更新(Laravel 5.x):

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

这篇关于如何创建 Laravel 哈希密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆