]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/utils/MWCryptRand.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / utils / MWCryptRand.php
1 <?php
2 /**
3  * A cryptographic random generator class used for generating secret keys
4  *
5  * This is based in part on Drupal code as well as what we used in our own code
6  * prior to introduction of this class.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  * http://www.gnu.org/copyleft/gpl.html
22  *
23  * @author Daniel Friesen
24  * @file
25  */
26
27 use MediaWiki\MediaWikiServices;
28
29 class MWCryptRand {
30         /**
31          * @return CryptRand
32          */
33         protected static function singleton() {
34                 return MediaWikiServices::getInstance()->getCryptRand();
35         }
36
37         /**
38          * Return a boolean indicating whether or not the source used for cryptographic
39          * random bytes generation in the previously run generate* call
40          * was cryptographically strong.
41          *
42          * @return bool Returns true if the source was strong, false if not.
43          */
44         public static function wasStrong() {
45                 return self::singleton()->wasStrong();
46         }
47
48         /**
49          * Generate a run of (ideally) cryptographically random data and return
50          * it in raw binary form.
51          * You can use MWCryptRand::wasStrong() if you wish to know if the source used
52          * was cryptographically strong.
53          *
54          * @param int $bytes The number of bytes of random data to generate
55          * @param bool $forceStrong Pass true if you want generate to prefer cryptographically
56          *                          strong sources of entropy even if reading from them may steal
57          *                          more entropy from the system than optimal.
58          * @return string Raw binary random data
59          */
60         public static function generate( $bytes, $forceStrong = false ) {
61                 return self::singleton()->generate( $bytes, $forceStrong );
62         }
63
64         /**
65          * Generate a run of (ideally) cryptographically random data and return
66          * it in hexadecimal string format.
67          * You can use MWCryptRand::wasStrong() if you wish to know if the source used
68          * was cryptographically strong.
69          *
70          * @param int $chars The number of hex chars of random data to generate
71          * @param bool $forceStrong Pass true if you want generate to prefer cryptographically
72          *                          strong sources of entropy even if reading from them may steal
73          *                          more entropy from the system than optimal.
74          * @return string Hexadecimal random data
75          */
76         public static function generateHex( $chars, $forceStrong = false ) {
77                 return self::singleton()->generateHex( $chars, $forceStrong );
78         }
79 }