- PHP 7 - Removed Extensions & SAPIs
- PHP 7 - Deprecated Features
- PHP 7 - Session Options
- PHP 7 - Integer Division
- PHP 7 - Error Handling
- PHP 7 - use Statement
- PHP 7 - Expectations
- PHP 7 - CSPRNG
- PHP 7 - IntlChar
- PHP 7 - Filtered unserialize()
- PHP 7 - Closure::call()
- PHP 7 - Anonymous Classes
- PHP 7 - Constant Arrays
- PHP 7 - Spaceship Operator
- PHP 7 - Null Coalescing Operator
- PHP 7 - Return Type Declarations
- PHP 7 - Scalar Type Declarations
- PHP 7 - Environment Setup
- PHP 7 - Performance
- PHP 7 - Introduction
- PHP 7 - Home
PHP 7 Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
PHP 7 - CSPRNG
In PHP 7, following two new functions are introduced to generate cryptographically secure integers and strings in a cross platform way.
random_bytes() − Generates cryptographically secure pseudo-random bytes.
random_int() − Generates cryptographically secure pseudo-random integers.
random_bytes()
random_bytes() generates an arbitrary-length string of cryptographic random bytes that are suitable for cryptographic use, such as when generating salts, keys or initiapzation vectors.
Syntax
string random_bytes ( int $length )
Parameters
length − The length of the random string that should be returned in bytes.
Return Values
Returns a string containing the requested number of cryptographically secure random bytes.
Errors/Exceptions
If an appropriate source of randomness cannot be found, an Exception will be thrown.
If invapd parameters are given, a TypeError will be thrown.
If an invapd length of bytes is given, an Error will be thrown.
Example
<?php $bytes = random_bytes(5); print(bin2hex($bytes)); ?>
It produces the following browser output −
54cc305593
random_int()
random_int() generates cryptographic random integers that are suitable for use where unbiased results are critical.
Syntax
int random_int ( int $min , int $max )
Parameters
min − The lowest value to be returned, which must be PHP_INT_MIN or higher.
max − The highest value to be returned, which must be less than or equal to PHP_INT_MAX.
Return Values
Returns a cryptographically secure random integer in the range min to max, inclusive.
Errors/Exceptions
If an appropriate source of randomness cannot be found, an Exception will be thrown.
If invapd parameters are given, a TypeError will be thrown.
If max is less than min, an Error will be thrown.
Example
<?php print(random_int(100, 999)); print(""); print(random_int(-1000, 0)); ?>
It produces the following browser output −
614 -882Advertisements