da0cae250d9edfbc359710be9c0ce5053fdce807
[lhc/web/wiklou.git] / includes / libs / CryptRand.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 /**
28 * @deprecated since 1.32, use random_bytes()/random_int()
29 */
30 class CryptRand {
31 /**
32 * @deprecated since 1.32, unused
33 */
34 const MIN_ITERATIONS = 1000;
35
36 /**
37 * @deprecated since 1.32, unused
38 */
39 const MSEC_PER_BYTE = 0.5;
40
41 /**
42 * Initialize an initial random state based off of whatever we can find
43 *
44 * @deprecated since 1.32, unused and does nothing
45 *
46 * @return string
47 */
48 protected function initialRandomState() {
49 wfDeprecated( __METHOD__, '1.32' );
50 return '';
51 }
52
53 /**
54 * Randomly hash data while mixing in clock drift data for randomness
55 *
56 * @deprecated since 1.32, unused and does nothing
57 *
58 * @param string $data The data to randomly hash.
59 * @return string The hashed bytes
60 * @author Tim Starling
61 */
62 protected function driftHash( $data ) {
63 wfDeprecated( __METHOD__, '1.32' );
64 return '';
65 }
66
67 /**
68 * Return a rolling random state initially build using data from unstable sources
69 *
70 * @deprecated since 1.32, unused and does nothing
71 *
72 * @return string A new weak random state
73 */
74 protected function randomState() {
75 wfDeprecated( __METHOD__, '1.32' );
76 return '';
77 }
78
79 /**
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.
83 *
84 * @deprecated since 1.32, always returns true
85 *
86 * @return bool Always true
87 */
88 public function wasStrong() {
89 wfDeprecated( __METHOD__, '1.32' );
90 return true;
91 }
92
93 /**
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.
98 *
99 * @param int $bytes The number of bytes of random data to generate
100 * @return string Raw binary random data
101 */
102 public function generate( $bytes ) {
103 wfDeprecated( __METHOD__, '1.32' );
104 $bytes = floor( $bytes );
105 return random_bytes( $bytes );
106 }
107
108 /**
109 * Generate a run of cryptographically random data and return
110 * it in hexadecimal string format.
111 *
112 * @param int $chars The number of hex chars of random data to generate
113 * @return string Hexadecimal random data
114 */
115 public function generateHex( $chars ) {
116 wfDeprecated( __METHOD__, '1.32' );
117 return MWCryptRand::generateHex( $chars );
118 }
119 }