dongxia4880 2016-10-23 09:04
浏览 71

mcrypt已被弃用? - 如何在PHP中正确加密和保存密码?

I was told, that php mcrypt is deprecated and I should use a different method to hash and salt my passwords.

This is what I do currently:

public function saveNewUser(array $data) {
  $passwd = $this->mysqli->real_escape_string($datas['passwd']);
  $options = [
      'cost' => 11,
      'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM)
  ];

  $hashed_passwd = password_hash($passwd, PASSWORD_BCRYPT, $options);
  $this->optin_hash = md5(rand());
  //...
  //save user in DB with hashed passwd

Login:

if (password_verify($_POST['user_password'], $result_row->gmw_usr_passwd)) {//do some login stuff}

1.) What is the latest and most secure way to crypt and save a password? Can you give an usage example or link how to save crypt a password correctly and how to verify it for login?

2.) In the php Documentary I read something about password_hash:

password_hash() creates a new password hash using a strong one-way hashing algorithm. password_hash() is compatible with crypt(). Therefore, password hashes created by crypt() can be used with password_hash().

(...)

Warning The salt option has been deprecated as of PHP 7.0.0. It is now preferred to simply use the salt that is generated by default.

2.a) Is password_hash an alternative to what I used?

2.b) So I don't need to add salt by myself?

2.c) What about that blowfish algorythm I used and all the other steps I added? Are they not neccesary anymore?

2.d) how do I verify the passwords for login, when I use password_hash?

EDIT: Sorry I saw that I already use password_hash (it was a very short coding-night).

As described by Artjom B. I don't need mcrypt (?)

  • 写回答

1条回答 默认 最新

  • dongya8378 2016-10-23 09:23
    关注

    The new standard way, according to PHP documentation for PHP 7.0, is to use password_hash to hash the original password and then password_verify at login time, to verify the correctness of the provided password.

    These function are wrappers around the fundamentals, like crypt() and are recommended because they take care of things that you and I will never think about, like choosing the correct source of randomness for generating the salt (you can't use a standard rand function for encryption).

    Coming to 2b and the rest, you don't need to add the salt yourself because it generated by PHP and included in the password, and all the necessary steps are done for you.

    You just need to save the hashed password, created with password_hash, on the database and then use it, at login time, to compare it with the user-supplied password using password_verify.

    Also, yes mcrypt is deprecated, because it's not updated anymore.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!