# random_compat
[![Build Status](https://travis-ci.org/paragonie/random_compat.svg?branch=master)](https://travis-ci.org/paragonie/random_compat)
[![Scrutinizer](https://scrutinizer-ci.com/g/paragonie/random_compat/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/paragonie/random_compat)
PHP 5.x polyfill for `random_bytes()` and `random_int()` created and maintained
by [Paragon Initiative Enterprises](https://paragonie.com).
Although this library *should* function in earlier versions of PHP, we will only
consider issues relevant to [supported PHP versions](https://secure.php.net/supported-versions.php).
**If you are using an unsupported version of PHP, please upgrade as soon as possible.**
## Important
Although this library has been examined by some security experts in the PHP
community, there will always be a chance that we overlooked something. Please
ask your favorite trusted hackers to hammer it for implementation errors and
bugs before even thinking about deploying it in production.
**Do not use the master branch, use a [stable release](https://github.com/paragonie/random_compat/releases/latest).**
For the background of this library, please refer to our blog post on
[Generating Random Integers and Strings in PHP](https://paragonie.com/blog/2015/07/how-safely-generate-random-strings-and-integers-in-php).
### Usability Notice
If PHP cannot safely generate random data, this library will throw an `Exception`.
It will never fall back to insecure random data. If this keeps happening, upgrade
to a newer version of PHP immediately.
## Installing
**With [Composer](https://getcomposer.org):**
composer require paragonie/random_compat
**Signed PHP Archive:**
As of version 1.2.0, we also ship an ECDSA-signed PHP Archive with each stable
release on Github.
1. Download [the `.phar`, `.phar.pubkey`, and `.phar.pubkey.asc`](https://github.com/paragonie/random_compat/releases/latest) files.
2. (**Recommended** but not required) Verify the PGP signature of `.phar.pubkey`
(contained within the `.asc` file) using the [PGP public key for Paragon Initiative Enterprises](https://paragonie.com/static/gpg-public-key.txt).
3. Extract both `.phar` and `.phar.pubkey` files to the same directory.
4. `require_once "/path/to/random_compat.phar";`
5. When a new version is released, you only need to replace the `.phar` file;
the `.pubkey` will not change (unless our signing key is ever compromised).
**Manual Installation:**
1. Download [a stable release](https://github.com/paragonie/random_compat/releases/latest).
2. Extract the files into your project.
3. `require_once "/path/to/random_compat/lib/random.php";`
## Usage
This library exposes the [CSPRNG functions added in PHP 7](https://secure.php.net/manual/en/ref.csprng.php)
for use in PHP 5 projects. Their behavior should be identical.
### Generate a string of random bytes
```php
try {
$string = random_bytes(32);
} catch (TypeError $e) {
// Well, it's an integer, so this IS unexpected.
die("An unexpected error has occurred");
} catch (Error $e) {
// This is also unexpected because 32 is a reasonable integer.
die("An unexpected error has occurred");
} catch (Exception $e) {
// If you get this message, the CSPRNG failed hard.
die("Could not generate a random string. Is our OS secure?");
}
var_dump(bin2hex($string));
// string(64) "5787c41ae124b3b9363b7825104f8bc8cf27c4c3036573e5f0d4a91ad2eeac6f"
```
### Generate a random integer between two given integers (inclusive)
```php
try {
$int = random_int(0,255);
} catch (TypeError $e) {
// Well, it's an integer, so this IS unexpected.
die("An unexpected error has occurred");
} catch (Error $e) {
// This is also unexpected because 0 and 255 are both reasonable integers.
die("An unexpected error has occurred");
} catch (Exception $e) {
// If you get this message, the CSPRNG failed hard.
die("Could not generate a random string. Is our OS secure?");
}
var_dump($int);
// int(47)
```
### Exception handling
When handling exceptions and errors you must account for differences between
PHP 5 and PHP7.
The differences:
* Catching `Error` works, so long as it is caught before `Exception`.
* Catching `Exception` has different behavior, without previously catching `Error`.
* There is *no* portable way to catch all errors/exceptions.
#### Our recommendation
**Always** catch `Error` before `Exception`.
#### Example
```php
try {
return random_int(1, $userInput);
} catch (TypeError $e) {
// This is okay, so long as `Error` is caught before `Exception`.
throw new Exception('Please enter a number!');
} catch (Error $e) {
// This is required, if you do not need to do anything just rethrow.
throw $e;
} catch (Exception $e) {
// This is optional and maybe omitted if you do not want to handle errors
// during generation.
throw new InternalServerErrorException(
'Oops, our server is bust and cannot generate any random data.',
500,
$e
);
}
```
## Contributors
This project would not be anywhere near as excellent as it is today if it
weren't for the contributions of the following individuals:
* [@AndrewCarterUK (Andrew Carter)](https://github.com/AndrewCarterUK)
* [@asgrim (James Titcumb)](https://github.com/asgrim)
* [@bcremer (Benjamin Cremer)](https://github.com/bcremer)
* [@CodesInChaos (Christian Winnerlein)](https://github.com/CodesInChaos)
* [@chriscct7 (Chris Christoff)](https://github.com/chriscct7)
* [@cs278 (Chris Smith)](https://github.com/cs278)
* [@cweagans (Cameron Eagans)](https://github.com/cweagans)
* [@dd32 (Dion Hulse)](https://github.com/dd32)
* [@geggleto (Glenn Eggleton)](https://github.com/geggleto)
* [@ircmaxell (Anthony Ferrara)](https://github.com/ircmaxell)
* [@jedisct1 (Frank Denis)](https://github.com/jedisct1)
* [@juliangut (Julián Gutiérrez)](https://github.com/juliangut)
* [@kelunik (Niklas Keller)](https://github.com/kelunik)
* [@lt (Leigh)](https://github.com/lt)
* [@MasonM (Mason Malone)](https://github.com/MasonM)
* [@mmeyer2k (Michael M)](https://github.com/mmeyer2k)
* [@narfbg (Andrey Andreev)](https://github.com/narfbg)
* [@nicolas-grekas (Nicolas Grekas)](https://github.com/nicolas-grekas)
* [@oittaa](https://github.com/oittaa)
* [@oucil (Kevin Farley)](https://github.com/oucil)
* [@redragonx (Stephen Chavez)](https://github.com/redragonx)
* [@rchouinard (Ryan Chouinard)](https://github.com/rchouinard)
* [@SammyK (Sammy Kaye Powers)](https://github.com/SammyK)
* [@scottchiefbaker (Scott Baker)](https://github.com/scottchiefbaker)
* [@skyosev (Stoyan Kyosev)](https://github.com/skyosev)
* [@stof (Christophe Coevoet)](https://github.com/stof)
* [@teohhanhui (Teoh Han Hui)](https://github.com/teohhanhui)
* [@tom-- (Tom Worster)](https://github.com/tom--)
* [@tsyr2ko](https://github.com/tsyr2ko)
* [@trowski (Aaron Piotrowski)](https://github.com/trowski)
* [@twistor (Chris Lepannen)](https://github.com/twistor)
* [@voku (Lars Moelleken)](https://github.com/voku)
* [@xabbuh (Christian Flothmann)](https://github.com/xabbuh)
没有合适的资源?快使用搜索试试~ 我知道了~
Laravel开发-laravel-sqs-jobless
共189个文件
php:163个
md:7个
json:6个
需积分: 9 1 下载量 182 浏览量
2019-08-27
19:49:00
上传
评论
收藏 614KB ZIP 举报
温馨提示
Laravel开发-laravel-sqs-jobless 从Amazon SQS接收自定义消息
资源推荐
资源详情
资源评论
收起资源包目录
Laravel开发-laravel-sqs-jobless (189个子文件)
random_compat.phar.pubkey.asc 488B
phpunit.xml.dist 821B
.gitignore 48B
.gitignore 0B
installed.json 7KB
composer.json 1KB
composer.json 1021B
composer.json 970B
composer.json 696B
composer.json 590B
LICENSE 1KB
LICENSE 1KB
LICENSE 1KB
composer.lock 8KB
CHANGELOG.md 10KB
README.md 7KB
SECURITY.md 5KB
README.md 3KB
ERRATA.md 1KB
README.md 287B
readme.md 0B
composer.phar 1.63MB
Collection.php 31KB
helpers.php 20KB
Str.php 19KB
Inflector.php 17KB
Arr.php 12KB
ClassLoader.php 12KB
InflectorTest.php 10KB
MessageBag.php 9KB
random.php 7KB
byte_safe_strings.php 6KB
random_int.php 6KB
Facade.php 5KB
ServiceProvider.php 5KB
MailFake.php 5KB
QueueFake.php 5KB
EventFake.php 5KB
random_bytes_dev_urandom.php 4KB
NotificationFake.php 4KB
ResponseFactory.php 4KB
Fluent.php 4KB
Filesystem.php 4KB
Container.php 3KB
NamespacedItemResolver.php 3KB
Manager.php 3KB
BusFake.php 3KB
Repository.php 3KB
random_bytes_libsodium.php 3KB
random_bytes_libsodium_legacy.php 3KB
random_bytes_com_dotnet.php 2KB
Registrar.php 2KB
Gate.php 2KB
cast_to_int.php 2KB
autoload_real.php 2KB
ClassLoader.php 2KB
random_bytes_mcrypt.php 2KB
Pluralizer.php 2KB
Composer.php 2KB
Log.php 2KB
ViewErrorBag.php 2KB
Paginator.php 2KB
Application.php 2KB
Macroable.php 2KB
MessageBag.php 2KB
Dispatcher.php 2KB
Store.php 2KB
autoload_static.php 2KB
Queue.php 2KB
Factory.php 2KB
build_phar.php 2KB
UrlGenerator.php 2KB
PasswordBroker.php 2KB
StatefulGuard.php 1KB
error_polyfill.php 1KB
Job.php 1KB
CapsuleManagerTrait.php 1KB
UserProvider.php 1KB
MailableFake.php 1KB
Factory.php 1KB
Factory.php 1KB
Repository.php 1KB
Password.php 1KB
JoblessQueue.php 1KB
Kernel.php 1KB
AggregateServiceProvider.php 1000B
Guard.php 966B
Authenticatable.php 948B
HtmlDumper.php 918B
JoblessJob.php 896B
Kernel.php 880B
Validator.php 841B
Cookie.php 834B
JoblessSQSServiceProvider.php 796B
JoblessConnector.php 795B
Hasher.php 775B
Factory.php 770B
Broadcaster.php 761B
Pipeline.php 759B
ExceptionHandler.php 759B
共 189 条
- 1
- 2
资源评论
weixin_38744270
- 粉丝: 328
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功