2012-03-20 05:17:40 +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.
|
|
|
|
|
*
|
2012-05-21 19:56:04 +00:00
|
|
|
* 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
|
|
|
|
|
*
|
2012-03-20 05:17:40 +00:00
|
|
|
* @author Daniel Friesen
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-02 07:04:17 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2012-03-20 05:17:40 +00:00
|
|
|
|
2016-10-02 07:04:17 +00:00
|
|
|
class MWCryptRand {
|
2012-03-20 05:17:40 +00:00
|
|
|
/**
|
2016-10-02 07:04:17 +00:00
|
|
|
* @return CryptRand
|
2012-03-20 05:17:40 +00:00
|
|
|
*/
|
|
|
|
|
protected static function singleton() {
|
2016-10-02 07:04:17 +00:00
|
|
|
return MediaWikiServices::getInstance()->getCryptRand();
|
2012-03-20 05:17:40 +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.
|
|
|
|
|
*
|
|
|
|
|
* @return bool Returns true if the source was strong, false if not.
|
|
|
|
|
*/
|
|
|
|
|
public static function wasStrong() {
|
2016-10-02 07:04:17 +00:00
|
|
|
return self::singleton()->wasStrong();
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a run of (ideally) cryptographically random data and return
|
|
|
|
|
* it in raw binary form.
|
|
|
|
|
* You can use MWCryptRand::wasStrong() if you wish to know if the source used
|
|
|
|
|
* was cryptographically strong.
|
|
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param int $bytes The number of bytes of random data to generate
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param bool $forceStrong Pass true if you want generate to prefer cryptographically
|
2012-03-20 05:17:40 +00:00
|
|
|
* strong sources of entropy even if reading from them may steal
|
|
|
|
|
* more entropy from the system than optimal.
|
2014-07-21 18:58:14 +00:00
|
|
|
* @return string Raw binary random data
|
2012-03-20 05:17:40 +00:00
|
|
|
*/
|
2012-03-21 10:27:34 +00:00
|
|
|
public static function generate( $bytes, $forceStrong = false ) {
|
2016-10-02 07:04:17 +00:00
|
|
|
return self::singleton()->generate( $bytes, $forceStrong );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a run of (ideally) cryptographically random data and return
|
|
|
|
|
* it in hexadecimal string format.
|
|
|
|
|
* You can use MWCryptRand::wasStrong() if you wish to know if the source used
|
|
|
|
|
* was cryptographically strong.
|
|
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param int $chars The number of hex chars of random data to generate
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param bool $forceStrong Pass true if you want generate to prefer cryptographically
|
2012-03-20 05:17:40 +00:00
|
|
|
* strong sources of entropy even if reading from them may steal
|
|
|
|
|
* more entropy from the system than optimal.
|
2014-07-21 18:58:14 +00:00
|
|
|
* @return string Hexadecimal random data
|
2012-03-20 05:17:40 +00:00
|
|
|
*/
|
2012-03-21 10:27:34 +00:00
|
|
|
public static function generateHex( $chars, $forceStrong = false ) {
|
2016-10-02 07:04:17 +00:00
|
|
|
return self::singleton()->generateHex( $chars, $forceStrong );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
}
|