doulangdang9986 2013-04-18 21:23
浏览 84

用php的crypt创建随机盐

Usally we use mt_rand to create a random salt to use it with crypt(). But according to mt_rand manual page on php's site "not be used for cryptographic purposes[...]consider using openssl_random_pseudo_bytes() instead." Also, at crypt manual page on php's site, someone suggest to use the

mcrypt_create_iv

So, to test them, I took this crypt's wrapper and change the following line

$salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mt_rand(0, 63), 1);

//change it to
$salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", openssl_random_pseudo_bytes(63, $cstrong), 50); 

$salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mcrypt_create_iv(63, MCRYPT_RAND), 50); 

$salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mt_srand(), 1);

Then I commented all the lines except one and start running the code, to check each function. I refreshed my page and the validation works fine. But when I run openssl_random_pseudo_bytes or mcrypt_create_iv I see the same hash frequently.

$2y$08$$$$$$$$$$$$$$$$$$$$$$.UrC6Lo4LNk8iLmoi25KEoVzHHTK7tNC

I saw the above hash like 10 times.

When I use mt_srand the hash never changes at all.

I test the same functions in another, simpler wrapper, found here and they act the same as I described above.

I am a begginer with hashing and crypt. I'm confused, what should I actually use?

  • 写回答

1条回答 默认 最新

  • dongzhi4470 2013-04-18 21:31
    关注

    Using openssl_random_pseudo_bytes() is about the best you can do in php to get random bytes. I'm not sure what you're trying to do with the substr(), as openssl_random_pseudo_bytes() returns bytes, not an offset.

    To clarify, you could use openssl_random_pseudo_bytes(64) on its own to generate a 64 byte salt.

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序