Skip to content

Commit

Permalink
Merge branch 'v1.x' of https://github.com/paragonie/random_compat int…
Browse files Browse the repository at this point in the history
…o v1.x

# Conflicts:
#	lib/random_bytes_com_dotnet.php
#	lib/random_bytes_dev_urandom.php
#	lib/random_bytes_libsodium_legacy.php
#	lib/random_bytes_openssl.php
  • Loading branch information
paragonie-security committed Mar 13, 2017
2 parents b078f63 + 572a26a commit d3fde68
Showing 1 changed file with 20 additions and 37 deletions.
57 changes: 20 additions & 37 deletions lib/random_bytes_dev_urandom.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php
/**
* Random_* Compatibility Library
* Random_* Compatibility Library
* for using the new PHP 7 random_* API in PHP 5 projects
*
*
* The MIT License (MIT)
*
* Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
*
*
* Copyright (c) 2015 Paragon Initiative Enterprises
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -67,16 +67,16 @@ function random_bytes($bytes)
if (!empty($fp)) {
/**
* stream_set_read_buffer() does not exist in HHVM
*
*
* If we don't set the stream's read buffer to 0, PHP will
* internally buffer 8192 bytes, which can waste entropy
*
*
* stream_set_read_buffer returns 0 on success
*/
if (is_callable('stream_set_read_buffer')) {
if (function_exists('stream_set_read_buffer')) {
stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER);
}
if (is_callable('stream_set_chunk_size')) {
if (function_exists('stream_set_chunk_size')) {
stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER);
}
}
Expand Down Expand Up @@ -104,50 +104,33 @@ function random_bytes($bytes)
* page load.
*/
if (!empty($fp)) {
/**
* @var int
*/
$remaining = $bytes;

/**
* @var string|bool
*/
$buf = '';

/**
* We use fread() in a loop to protect against partial reads
*/
do {
/**
* @var string|bool
*/
$read = fread($fp, $remaining);
if (!is_string($read)) {
if ($read === false) {
/**
* We cannot safely read from the file. Exit the
* do-while loop and trigger the exception condition
*
* @var string|bool
*/
$buf = false;
break;
}
if ($read === false) {
/**
* We cannot safely read from the file. Exit the
* do-while loop and trigger the exception condition
*/
$buf = false;
break;
}
/**
* Decrease the number of bytes returned from remaining
*/
$remaining -= RandomCompat_strlen($read);
/**
* @var string|bool
*/
$buf = $buf . $read;
$buf .= $read;
} while ($remaining > 0);

/**
* Is our result valid?
*/
if (is_string($buf)) {
if ($buf !== false) {
if (RandomCompat_strlen($buf) === $bytes) {
/**
* Return our random entropy buffer here:
Expand Down

0 comments on commit d3fde68

Please sign in to comment.