OAuthProvider::generateToken
(PECL OAuth >= 1.0.0)
OAuthProvider::generateToken — Generate a random token
Описание
$size
[, bool $strong
= false
] )Generates a string of pseudo-random bytes.
Список параметров
-
size
-
The desired token length, in terms of bytes.
-
strong
-
Setting to
TRUE
means /dev/random will be used for entropy, as otherwise the non-blocking /dev/urandom is used. This parameter is ignored on Windows.
Возвращаемые значения
The generated token, as a string of bytes.
Ошибки
If the strong
parameter is TRUE
, then an
E_WARNING
level error will be emitted when the
fallback rand() implementation is used to fill
the remaining random bytes (e.g., when not enough random data was found,
initially).
Примеры
Пример #1 OAuthProvider::generateToken() example
<?php
$p = new OAuthProvider();
$t = $p->generateToken(4);
echo strlen($t), PHP_EOL;
echo bin2hex($t), PHP_EOL;
?>
Результатом выполнения данного примера будет что-то подобное:
4 b6a82c27
Примечания
Замечание:
When not enough random data is available to the system, this function will fill the remaining random bytes using the internal PHP rand() implementation.
Смотрите также
- openssl_random_pseudo_bytes() - Generate a pseudo-random string of bytes
- mcrypt_create_iv() - Creates an initialization vector (IV) from a random source
- Функция OAuthProvider::addRequiredParameter() - Add required parameters
- Функция OAuthProvider::callconsumerHandler() - Calls the consumerNonceHandler callback
- Функция OAuthProvider::callTimestampNonceHandler() - Calls the timestampNonceHandler callback
- Функция OAuthProvider::calltokenHandler() - Calls the tokenNonceHandler callback
- Функция OAuthProvider::checkOAuthRequest() - Check an oauth request
- Функция OAuthProvider::__construct() - Constructs a new OAuthProvider object
- Функция OAuthProvider::consumerHandler() - Set the consumerHandler handler callback
- Функция OAuthProvider::generateToken() - Generate a random token
- Функция OAuthProvider::is2LeggedEndpoint() - is2LeggedEndpoint
- Функция OAuthProvider::isRequestTokenEndpoint() - Sets isRequestTokenEndpoint
- Функция OAuthProvider::removeRequiredParameter() - Remove a required parameter
- Функция OAuthProvider::reportProblem() - Report a problem
- Функция OAuthProvider::setParam() - Set a parameter
- Функция OAuthProvider::setRequestTokenPath() - Set request token path
- Функция OAuthProvider::timestampNonceHandler() - Set the timestampNonceHandler handler callback
- Функция OAuthProvider::tokenHandler() - Set the tokenHandler handler callback
Коментарии
Be careful when setting the 'strong' parameter to true.
If you system doesn't have enough entropy your script will block which can cause timeouts in other parts of your code.
In my case, the most serious symptom was my script blocking when trying to read from /dev/random and causing a 'MySQL has gone away' error.
Hopefully this saves someone the trouble when deciding to use /dev/random entropy