duan41497 2013-08-14 18:02
浏览 12
已采纳

使用mcrypt获取密码salt。 本地Apache服务器上的执行时间很快,但Web主机上的执行时间非常慢

$salt=bin2hex(mcrypt_create_iv(64, MCRYPT_DEV_RANDOM));

This is my current setup for creating a password salt. I noticed that when I uploaded my register page to the web host it took about 1 minute for the page to execute, while on my local apache server it was instant. I narrowed down the problem to this line of code. Are you aware of any execution time problems with this function or is there a better way to get a salt string (128 length)?

  • 写回答

1条回答 默认 最新

  • douduocuima61392 2013-08-15 06:42
    关注

    The problem lies with the MCRYPT_DEV_RANDOM parameter, reading from this will block the server until enough entrophy is available. Instead you should use:

    $binarySalt = mcrypt_create_iv(64, MCRYPT_DEV_URANDOM);
    

    Reading from URANDOM is the better choice for generating a salt, it is initially feeded from RANDOM but it doesn't block if there is not enough entrophy available. This also prevents, that somebody can drain the servers' entrophy pool by using your function.

    Depending on what hash function you use, the bin2hex() function is not optimal, because the alphabet of the resulting string is limited. You should allow all characters that are accepted by the hash function.

    Since PHP now has its own api to generate BCrypt hashes, i would recommend to use this function directly password_hash(), it does take care of the creation of a safe salt. There is also a compatibility pack for earlier PHP versions.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度