php使用mcrypt_create_iv [英] php using mcrypt_create_iv
问题描述
我想在PHP中创建一个随机的整数和字符串,所以我决定使用mcrypt_create_iv。它是用手写的,它使用/ dev / random和/ dev / urandom来实现随机性,但我找不到一个关于如何使用这个函数生成随机Int和String的简单教程。我尝试了一些代码,但这个函数给了我不可读的字符。那么请你给我一个简单的例子说明我可以正确使用它吗?
I want to create a random ints and strings in PHP and so I decided to use mcrypt_create_iv. It is written in manual that it uses /dev/random and /dev/urandom for randomness but I can't find a simple tutorial on how to generate random Int and String using this function. I tried some code but this function gives me unreadable characters. So please can you give me a simple example of how properly I can use it?
推荐答案
如果你想生成随机数,使用 mt_rand
:
If you want to generate random numbers, use mt_rand
:
$random = mt_rand(0, 999999);
如果你想要一个字符串,你可以通过一个哈希函数传递得到的整数:
If you want a string, you can pass the resulting integer through a hash function:
$random = mt_rand(0, 999999);
$random_string = sha1($random);
mcrypt_create_iv
用于加密。这与您的要求完全无关。
mcrypt_create_iv
is used in cryptography. It is totally unrelated to your requirement.
如果您想在安全组件(盐,密码等)中使用随机字符串,请从<$ c中读取$ c> / dev / urandom 喜欢这样:
If you're looking to use the random string in security components (salts, passwords, etc.), read from /dev/urandom
like so:
$random = file_get_contents('/dev/urandom', false, null, 0, 10);
...其中 10
是长度,然后像这样转换:
...where 10
is the length, then convert like this:
$string = bin2hex($random);
$number = current(unpack('L', $random));
这篇关于php使用mcrypt_create_iv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!