dstew32424 2014-04-29 07:07
浏览 17
已采纳

为什么这么多密码教程在哈希密码之后使用“mysql_real_escape_string”?

I don't see the need, when the input is taken as a string and hashed. Wouldn't hashing it effectively, well, sanitize the input? It seems like a waste of about a couple hundred CPU cycles, not significant, but I don't see the need.

I realize I'll need to use crypt to protect my passwords.

Edit: As an example:

$password = $_POST['password'];
$password = strip_tags($password); 
$password = sha1($password);

It makes no sense to me.

  • 写回答

1条回答 默认 最新

  • dppx9253 2014-04-29 07:34
    关注

    The Internet is full of chimpanzees

    That's the main reason why it is always mentioned.
    In these tutorials, where only raw PHP without even grain of abstraction is used, it's indeed superfluous.

    However, in fact, in a real application mysql_real_escape_string or equivalent have to be applied. Not the way it is described in aforementioned tutorials but anyway.

    In a sanely designed application database layer have to be separated from other logic. And by the time of the query execution it have to be totally ignorant of the data source or nature. Means all the data have to be treated the same way. I.e. strings have to be quoted and escaped.

    Thus, your password have to be sent to database layer, and treated there as a regular string. And, depends on the layer architecture, it may be escaped and quoted, or sent as a parameter separated from the query, or encoded some way - but that shouldn't be our concern at all.

    Wouldn't hashing it effectively, well, sanitize the input?

    Note the emphasized word.

    Your particular confusion coming from the misinterpreting the purpose of mysql_real_escape_string or similar function. In fact, it does sanitize nothing. What it really does is part of required formatting. A string, being placed into SQL query, have to be properly formatted. Always. Despite of the source, contents, or constellations interposition.

    After all, some hashing function may return binary string that pretty much can contain a special character. But again - why bother at all? These CPU cycles don't worth a fraction of overall sanity and security of application.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示