本文整理汇总了PHP中Crypto::random方法的典型用法代码示例。如果您正苦于以下问题:PHP Crypto::random方法的具体用法?PHP Crypto::random怎么用?PHP Crypto::random使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto
的用法示例。
在下文中一共展示了Crypto::random方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testRandom
function testRandom()
{
for ($i = 1; $i < 128; $i += 4) {
$data = Crypto::random($i);
$this->assertNotEqual($data, '', 'Empty random data generated');
$this->assert(strlen($data) == $i, 'Random data received was not the length requested');
}
}
示例2: getToken
function getToken()
{
if (!$this->csrf['token'] || $this->isExpired()) {
$this->csrf['token'] = sha1(session_id() . Crypto::random(16) . SECRET_SALT);
$this->csrf['time'] = time();
} else {
//Reset the timer
$this->csrf['time'] = time();
}
return $this->csrf['token'];
}
示例3: randCode
function randCode($len = 8, $chars = false)
{
$chars = $chars ?: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_=';
// Determine the number of bits we need
$char_count = strlen($chars);
$bits_per_char = ceil(log($char_count, 2));
$bytes = ceil(4 * $len / floor(32 / $bits_per_char));
// Pad to 4 byte boundary
$bytes += (4 - $bytes % 4) % 4;
// Fetch some random data blocks
$data = Crypto::random($bytes);
$mask = (1 << $bits_per_char) - 1;
$loops = (int) (32 / $bits_per_char);
$output = '';
$ints = unpack('V*', $data);
foreach ($ints as $int) {
for ($i = $loops; $i > 0; $i--) {
$output .= $chars[($int & $mask) % $char_count];
$int >>= $bits_per_char;
}
}
return substr($output, 0, $len);
}
示例4: rotate
function rotate()
{
$this->csrf['token'] = sha1(session_id() . Crypto::random(16) . SECRET_SALT);
$this->csrf['time'] = time();
}
示例5: encrypt
function encrypt($text, $cid = 0)
{
if (!$this->exists() || !$text || !($cipher = $this->getCipher($cid)) || !($crypto = $this->getCrypto($cipher['cid']))) {
return false;
}
$ivlen = $cipher['ivlen'];
$iv = Crypto::random($ivlen);
$crypto->setKey($this->getKeyHash($iv, $ivlen));
$crypto->setIV($iv);
return sprintf('$%s$%s%s', $cipher['cid'], $iv, $crypto->encrypt($text));
}