2016-10-02 07:04:17 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* A cryptographic random generator class used for generating secret keys
|
|
|
|
|
*
|
|
|
|
|
* This is based in part on Drupal code as well as what we used in our own code
|
|
|
|
|
* prior to introduction of this class.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @author Daniel Friesen
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2018-01-19 22:42:56 +00:00
|
|
|
/**
|
|
|
|
|
* @deprecated since 1.32, use random_bytes()/random_int()
|
|
|
|
|
*/
|
2016-10-02 07:04:17 +00:00
|
|
|
class CryptRand {
|
|
|
|
|
/**
|
2018-01-19 22:42:56 +00:00
|
|
|
* @deprecated since 1.32, unused
|
2016-10-02 07:04:17 +00:00
|
|
|
*/
|
|
|
|
|
const MIN_ITERATIONS = 1000;
|
|
|
|
|
|
|
|
|
|
/**
|
2018-01-19 22:42:56 +00:00
|
|
|
* @deprecated since 1.32, unused
|
2016-10-02 07:04:17 +00:00
|
|
|
*/
|
|
|
|
|
const MSEC_PER_BYTE = 0.5;
|
|
|
|
|
|
|
|
|
|
/**
|
2018-01-19 22:42:56 +00:00
|
|
|
* Initialize an initial random state based off of whatever we can find
|
2016-10-02 07:04:17 +00:00
|
|
|
*
|
2018-01-19 22:42:56 +00:00
|
|
|
* @deprecated since 1.32, unused and does nothing
|
2016-10-02 07:04:17 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function initialRandomState() {
|
2018-10-18 02:19:41 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2018-01-19 22:42:56 +00:00
|
|
|
return '';
|
2016-10-02 07:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Randomly hash data while mixing in clock drift data for randomness
|
|
|
|
|
*
|
2018-01-19 22:42:56 +00:00
|
|
|
* @deprecated since 1.32, unused and does nothing
|
|
|
|
|
*
|
2016-10-02 07:04:17 +00:00
|
|
|
* @param string $data The data to randomly hash.
|
|
|
|
|
* @return string The hashed bytes
|
|
|
|
|
* @author Tim Starling
|
|
|
|
|
*/
|
|
|
|
|
protected function driftHash( $data ) {
|
2018-10-18 02:19:41 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2018-01-19 22:42:56 +00:00
|
|
|
return '';
|
2016-10-02 07:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a rolling random state initially build using data from unstable sources
|
2018-01-19 22:42:56 +00:00
|
|
|
*
|
|
|
|
|
* @deprecated since 1.32, unused and does nothing
|
|
|
|
|
*
|
2016-10-02 07:04:17 +00:00
|
|
|
* @return string A new weak random state
|
|
|
|
|
*/
|
|
|
|
|
protected function randomState() {
|
2018-10-18 02:19:41 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2018-01-19 22:42:56 +00:00
|
|
|
return '';
|
2016-10-02 07:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a boolean indicating whether or not the source used for cryptographic
|
|
|
|
|
* random bytes generation in the previously run generate* call
|
|
|
|
|
* was cryptographically strong.
|
|
|
|
|
*
|
2018-01-19 22:42:56 +00:00
|
|
|
* @deprecated since 1.32, always returns true
|
|
|
|
|
*
|
|
|
|
|
* @return bool Always true
|
2016-10-02 07:04:17 +00:00
|
|
|
*/
|
|
|
|
|
public function wasStrong() {
|
2018-10-18 02:19:41 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2018-01-19 22:42:56 +00:00
|
|
|
return true;
|
2016-10-02 07:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-01-19 22:42:56 +00:00
|
|
|
* Generate a run of cryptographically random data and return
|
2016-10-02 07:04:17 +00:00
|
|
|
* it in raw binary form.
|
|
|
|
|
* You can use CryptRand::wasStrong() if you wish to know if the source used
|
|
|
|
|
* was cryptographically strong.
|
|
|
|
|
*
|
|
|
|
|
* @param int $bytes The number of bytes of random data to generate
|
|
|
|
|
* @return string Raw binary random data
|
|
|
|
|
*/
|
2018-01-19 22:42:56 +00:00
|
|
|
public function generate( $bytes ) {
|
2018-10-18 02:19:41 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2016-10-02 07:04:17 +00:00
|
|
|
$bytes = floor( $bytes );
|
2018-01-19 22:42:56 +00:00
|
|
|
return random_bytes( $bytes );
|
2016-10-02 07:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-01-19 22:42:56 +00:00
|
|
|
* Generate a run of cryptographically random data and return
|
2016-10-02 07:04:17 +00:00
|
|
|
* it in hexadecimal string format.
|
|
|
|
|
*
|
|
|
|
|
* @param int $chars The number of hex chars of random data to generate
|
|
|
|
|
* @return string Hexadecimal random data
|
|
|
|
|
*/
|
2018-01-19 22:42:56 +00:00
|
|
|
public function generateHex( $chars ) {
|
2018-10-18 02:19:41 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.32' );
|
2018-01-19 22:42:56 +00:00
|
|
|
return MWCryptRand::generateHex( $chars );
|
2016-10-02 07:04:17 +00:00
|
|
|
}
|
|
|
|
|
}
|