dongliechuich10319 2011-04-19 06:49
浏览 343
已采纳

使用SHA-256加密创建和检索密码的php命令是什么?

Newbie here. I'm trying to encrypt the user password using SHA-256 after they've made their account and then when the user tries to log in, it will try to match their input with any of the encrypted passwords in the mysql database. I was wondering if someone could give me an example code?

  • 写回答

2条回答 默认 最新

  • drqj8605 2011-04-19 06:55
    关注

    Hashing works one-way, so you cannot really 'retrieve' what you have hashed.

    When you save the password on registration, use something like this:

    $hash=hash('sha256', $password);
    

    And only save $hash in the database. When the user tries to log in, hash the password he tries to use and compare it to the one in the database (hashing will always give the same result for the same input). If they are the same, he can be logged in (if all additional checks are satisfied).

    If you want to let the user recover his forgotten pass, refer to this earlier question.

    Also, the best practice in hashing passwords is to use some kind of salt, which is out of the scope of this question, but please google it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备