1717 * @category Xmf\Random
1818 * @package Xmf
1919 * @author Richard Griffith <richard@geekwright.com>
20- * @copyright 2015-2018 XOOPS Project (https://xoops.org)
20+ * @copyright 2015-2023 XOOPS Project (https://xoops.org)
2121 * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
2222 * @link https://xoops.org
2323 */
@@ -37,29 +37,7 @@ class Random
3737 */
3838 public static function generateOneTimeToken ($ hash = 'sha512 ' , $ bytes = 64 )
3939 {
40- if (function_exists ('random_bytes ' )) {
41- $ randomData = random_bytes ($ bytes );
42- } elseif (function_exists ('openssl_random_pseudo_bytes ' )) {
43- $ crypto_strong = false ;
44- $ randomData = openssl_random_pseudo_bytes ($ bytes , $ crypto_strong );
45-
46- if ($ randomData === false ) {
47- throw new Exception ("Could not generate secure random bytes. " );
48- }
49-
50- if (!$ crypto_strong ) {
51- throw new Exception ("Non-cryptographically strong algorithm used for random bytes. " );
52- }
53- } else {
54- $ randomData = md5 (uniqid (mt_rand (), true ));
55- }
56-
57- if ($ randomData === null ) {
58- throw new Exception ("Failed to generate random data. " );
59- }
60-
61- $ token = hash ($ hash , $ randomData );
62-
40+ $ token = hash ($ hash , random_bytes ($ bytes ));
6341 return $ token ;
6442 }
6543
@@ -77,28 +55,7 @@ public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
7755 */
7856 public static function generateKey ($ hash = 'sha512 ' , $ bytes = 128 )
7957 {
80- if (function_exists ('random_bytes ' )) {
81- $ randomData = random_bytes ($ bytes );
82- } elseif (function_exists ('openssl_random_pseudo_bytes ' )) {
83- $ crypto_strong = false ;
84- $ randomData = openssl_random_pseudo_bytes ($ bytes , $ crypto_strong );
85-
86- if ($ randomData === false ) {
87- throw new Exception ("Could not generate secure random bytes. " );
88- }
89-
90- if (!$ crypto_strong ) {
91- throw new Exception ("Non-cryptographically strong algorithm used for random bytes. " );
92- }
93- } else {
94- $ randomData = md5 (uniqid (mt_rand (), true ));
95- }
96-
97- if ($ randomData === null ) {
98- throw new Exception ("Failed to generate random data. " );
99- }
100-
101- $ token = hash ($ hash , $ randomData );
58+ $ token = hash ($ hash , random_bytes ($ bytes ));
10259 return $ token ;
10360 }
10461}
0 commit comments