Merge "Fix 'Tags' padding to keep it farther from the edge and document the source...
[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 return '';
50 }
51
52 /**
53 * Randomly hash data while mixing in clock drift data for randomness
54 *
55 * @deprecated since 1.32, unused and does nothing
56 *
57 * @param string $data The data to randomly hash.
58 * @return string The hashed bytes
59 * @author Tim Starling
60 */
61 protected function driftHash( $data ) {
62 return '';
63 }
64
65 /**
66 * Return a rolling random state initially build using data from unstable sources
67 *
68 * @deprecated since 1.32, unused and does nothing
69 *
70 * @return string A new weak random state
71 */
72 protected function randomState() {
73 return '';
74 }
75
76 /**
77 * Return a boolean indicating whether or not the source used for cryptographic
78 * random bytes generation in the previously run generate* call
79 * was cryptographically strong.
80 *
81 * @deprecated since 1.32, always returns true
82 *
83 * @return bool Always true
84 */
85 public function wasStrong() {
86 return true;
87 }
88
89 /**
90 * Generate a run of cryptographically random data and return
91 * it in raw binary form.
92 * You can use CryptRand::wasStrong() if you wish to know if the source used
93 * was cryptographically strong.
94 *
95 * @param int $bytes The number of bytes of random data to generate
96 * @return string Raw binary random data
97 */
98 public function generate( $bytes ) {
99 $bytes = floor( $bytes );
100 return random_bytes( $bytes );
101 }
102
103 /**
104 * Generate a run of cryptographically random data and return
105 * it in hexadecimal string format.
106 *
107 * @param int $chars The number of hex chars of random data to generate
108 * @return string Hexadecimal random data
109 */
110 public function generateHex( $chars ) {
111 return MWCryptRand::generateHex( $chars );
112 }
113 }