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条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?