3 * A cryptographic random generator class used for generating secret keys
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.
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.
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.
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
23 * @author Daniel Friesen
28 * @deprecated since 1.32, use random_bytes()/random_int()
32 * @deprecated since 1.32, unused
34 const MIN_ITERATIONS
= 1000;
37 * @deprecated since 1.32, unused
39 const MSEC_PER_BYTE
= 0.5;
42 * Initialize an initial random state based off of whatever we can find
44 * @deprecated since 1.32, unused and does nothing
48 protected function initialRandomState() {
49 wfDeprecated( __METHOD__
, '1.32' );
54 * Randomly hash data while mixing in clock drift data for randomness
56 * @deprecated since 1.32, unused and does nothing
58 * @param string $data The data to randomly hash.
59 * @return string The hashed bytes
60 * @author Tim Starling
62 protected function driftHash( $data ) {
63 wfDeprecated( __METHOD__
, '1.32' );
68 * Return a rolling random state initially build using data from unstable sources
70 * @deprecated since 1.32, unused and does nothing
72 * @return string A new weak random state
74 protected function randomState() {
75 wfDeprecated( __METHOD__
, '1.32' );
80 * Return a boolean indicating whether or not the source used for cryptographic
81 * random bytes generation in the previously run generate* call
82 * was cryptographically strong.
84 * @deprecated since 1.32, always returns true
86 * @return bool Always true
88 public function wasStrong() {
89 wfDeprecated( __METHOD__
, '1.32' );
94 * Generate a run of cryptographically random data and return
95 * it in raw binary form.
96 * You can use CryptRand::wasStrong() if you wish to know if the source used
97 * was cryptographically strong.
99 * @param int $bytes The number of bytes of random data to generate
100 * @return string Raw binary random data
102 public function generate( $bytes ) {
103 wfDeprecated( __METHOD__
, '1.32' );
104 $bytes = floor( $bytes );
105 return random_bytes( $bytes );
109 * Generate a run of cryptographically random data and return
110 * it in hexadecimal string format.
112 * @param int $chars The number of hex chars of random data to generate
113 * @return string Hexadecimal random data
115 public function generateHex( $chars ) {
116 wfDeprecated( __METHOD__
, '1.32' );
117 return MWCryptRand
::generateHex( $chars );