本文整理汇总了PHP中Encrypt::rand方法的典型用法代码示例。如果您正苦于以下问题:PHP Encrypt::rand方法的具体用法?PHP Encrypt::rand怎么用?PHP Encrypt::rand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Encrypt
的用法示例。
在下文中一共展示了Encrypt::rand方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: encode
/**
* Encrypts a string and returns an encrypted string that can be decoded.
*
* @param string data to be encrypted
* @return string encrypted data
*/
public function encode($data)
{
// Set the rand type if it has not already been set
if (Encrypt::$rand === NULL) {
if (KOHANA_IS_WIN) {
// Windows only supports the system random number generator
Encrypt::$rand = MCRYPT_RAND;
} else {
if (defined('MCRYPT_DEV_URANDOM')) {
// Use /dev/urandom
Encrypt::$rand = MCRYPT_DEV_URANDOM;
} elseif (defined('MCRYPT_DEV_RANDOM')) {
// Use /dev/random
Encrypt::$rand = MCRYPT_DEV_RANDOM;
} else {
// Use the system random number generator
Encrypt::$rand = MCRYPT_RAND;
}
}
}
if (Encrypt::$rand === MCRYPT_RAND) {
// The system random number generator must always be seeded each
// time it is used, or it will not produce true random results
mt_srand();
}
// Create a random initialization vector of the proper size for the current cipher
$iv = mcrypt_create_iv($this->config['iv_size'], Encrypt::$rand);
// Encrypt the data using the configured options and generated iv
$data = mcrypt_encrypt($this->config['cipher'], $this->config['key'], $data, $this->config['mode'], $iv);
// Use base64 encoding to convert to a string
return base64_encode($iv . $data);
}