使用 Code Igniter 存储密码的最安全方法是什么?

发布于 2024-11-29 08:26:44 字数 1178 浏览 1 评论 0原文

我正在为我当前的项目使用 Code Igniter。

截至目前,我正在使用 MD5 进行密码散列,但我在很多地方读到,这样做不是一个好的做法。

我应该带什么去?

  1. 使用salt
  2. 或者我应该使用bcrypt

另外,如果推荐bcrypt,那么如何将它与Code Igniter一起使用?

编辑

我已将这些文件放入 application/libraries

  1. PasswordHash.php
  2. c/Makefile
  3. c/crypt_private.c

在我的控制器中,我正在使用此代码 -

$params = array(
       'phpass_hash_strength' => 8,
           'phpass_hash_portable' => FALSE
       );
$this->load->library('PasswordHash', $params);
$password = $this->passwordhash->HashPassword($pwd);

我收到这些错误 -

A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 3

Filename: libraries/PasswordHash.php

Line Number: 116

A PHP Error was encountered

Severity: Warning

Message: strpos() [function.strpos]: Empty delimiter

Filename: libraries/PasswordHash.php

Line Number: 116

更新

已删除 PasswordHash .php,现在使用SimpleLoginSecure

I am using Code Igniter for my current project.

As of now, I am using MD5 for password hashing, but I have read at a lot of places, that it is not a good practice to do so.

What should I go with?

  1. Using a salt
  2. Or should I use bcrypt

Also, if bcrypt is recommended, then how to use it with Code Igniter?

EDIT

I have put these files in application/libraries

  1. PasswordHash.php
  2. c/Makefile
  3. c/crypt_private.c

In my controller, I am using this code -

$params = array(
       'phpass_hash_strength' => 8,
           'phpass_hash_portable' => FALSE
       );
$this->load->library('PasswordHash', $params);
$password = $this->passwordhash->HashPassword($pwd);

I am getting these errors -

A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 3

Filename: libraries/PasswordHash.php

Line Number: 116

A PHP Error was encountered

Severity: Warning

Message: strpos() [function.strpos]: Empty delimiter

Filename: libraries/PasswordHash.php

Line Number: 116

Update

Removed PasswordHash.php, using SimpleLoginSecure now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記柔刀 2024-12-06 08:26:44

使用 bcrypt。这个讨论出现在这里在我的评论中回答。您可以使用 phppass 等库来真正简化密码加密。

关于盐的问题。使用它!否则,有人可以简单地访问此网站并下载涵盖绝大多数的彩虹表普通用户选择的密码。特别是考虑到过去几个月出现的所有安全漏洞,现在不是说您不会使用像随机盐这样简单的实现方式的时候。

更新

要将 PHPPass 与 CI 结合使用,请从上面链接的 phppass 网站下载并提取文件。将PasswordHash.php 文件放入您的CI application/libraries 目录中。

然后,在代码中,您可以通过以下方式加载库: $this->load->library('PasswordHash',array(8, FALSE));

哈希密码就像 $this->PasswordHash->HashPassword($password);

要稍后检查密码是否正确,很简单:

$password = $_POST['password'];
$actualPassword = /*Get the hashed password from your db*/;

$check = $this->PasswordHash->CheckPassword($password, $actualPassword);

我从 http://dev.myunv.com/articles/secure-passwords-with-phpass / 这为您提供了更多信息。我稍微修改了该教程以利用 CI 的加载器,这就是为什么您不需要 includenew 语句。

Use bcrypt. This discussion came up here in the comments to my answer. You can use a library such as phppass to really simplify the password encryption.

On the matter of salt. Use it! Otherwise somebody can simply go to this site and download the rainbow tables that will cover the large majority of passwords the average users chooses. Especially with all the security leaks in the last few months, now is not the time to be saying you won't use something as simple to implement as random salt.

UPDATE

To use PHPPass with CI, download and extract the files from the phppass website, linked above. Put the PasswordHash.php file into your CI application/libraries directory.

In your code, you then load the library via: $this->load->library('PasswordHash',array(8, FALSE));

Hashing passwords is then as simple as $this->PasswordHash->HashPassword($password);

To later check if a password is correct, it is as simple as:

$password = $_POST['password'];
$actualPassword = /*Get the hashed password from your db*/;

$check = $this->PasswordHash->CheckPassword($password, $actualPassword);

I've taken this demo from http://dev.myunv.com/articles/secure-passwords-with-phpass/ which gives you a lot more informations. I've modified that tutorial slightly to utilize CI's loader which is why you don't need the include or new statements.

っ左 2024-12-06 08:26:44

为什么要使用md5(),因为它同样易于使用<代码>sha1()

Table 攻击的威胁

另外,对密码加盐始终是一个好主意,因为它可以有效消除 Rainbow 根据我的经验,加盐 SHA1 哈希对于 99% 的 Web 应用程序情况来说都是非常安全的。

why use md5() when it is just as easy to use sha1() ?

Also salting the passwords is always a good idea as it effectively removes the threat of a Rainbow Table attack

In my experience a salted SHA1 hash is pleanty secure for 99% of web application situations.

十雾 2024-12-06 08:26:44

自从提出这个问题以来,Code Igniter 已经发生了变化。但为了那些可能没有接触过 CI 的大量文档或以前没有见过这一点的人的利益,CI 有一个加密类,它使用 PHP 的 Mcrypt 库提供双向数据加密。

使用以下方法初始化类后:

$this->load->library('encrypt');

您可以按如下方式加密:

$msg = 'My secret message';
$encrypted_string = $this->encrypt->encode($msg);

并按如下方式解密:

$encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';
$plaintext_string = $this->encrypt->decode($encrypted_string);

CI 还具有不可解码的单向哈希:

$hash = $this->encrypt->sha1('Some string');

有关详细信息,请参阅:
http://www.codeigniter.com/user_guide/libraries/encryption.html

Code Igniter has changed since the time this question was asked. But for the benefit of some who may not have come across the extensive documentation of CI or haven't seen this before, CI has an encryption class which provides a two-way data encryption using the Mcrypt library of PHP.

After initializing the class using:

$this->load->library('encrypt');

You can encrypt as follows:

$msg = 'My secret message';
$encrypted_string = $this->encrypt->encode($msg);

and decrypt as follows:

$encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';
$plaintext_string = $this->encrypt->decode($encrypted_string);

CI also has a non-decodable 1-way hashing:

$hash = $this->encrypt->sha1('Some string');

For more information see:
http://www.codeigniter.com/user_guide/libraries/encryption.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文