1: <?php
2: /*
3: * You may not change or alter any portion of this comment or credits
4: * of supporting developers from this source code or any supporting source code
5: * which is considered copyrighted (c) material of the original comment or credit authors.
6: *
7: * This program is distributed in the hope that it will be useful,
8: * but WITHOUT ANY WARRANTY; without even the implied warranty of
9: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xmf;
13:
14: /**
15: * XOOPS Random generator
16: *
17: * @category Xmf\Random
18: * @package Xmf
19: * @author Richard Griffith <richard@geekwright.com>
20: * @copyright 2015-2018 XOOPS Project (https://xoops.org)
21: * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22: * @link https://xoops.org
23: */
24: class Random
25: {
26: /**
27: * Create a one time token
28: *
29: * Generates a low strength random number of size $bytes and hash with the
30: * algorithm specified in $hash.
31: *
32: * @param string $hash hash function to use
33: * @param integer $bytes the number of random bit to generate
34: *
35: * @return string hashed token
36: * @throws \Exception on insufficient entropy
37: */
38: public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
39: {
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:
63: return $token;
64: }
65:
66: /**
67: * Create a medium strength key
68: *
69: * Generates a medium strength random number of size $bytes and hash with the
70: * algorithm specified in $hash.
71: *
72: * @param string $hash hash function to use
73: * @param integer $bytes the number of random bytes to generate
74: *
75: * @return string hashed token
76: * @throws \Exception on insufficient entropy
77: */
78: public static function generateKey($hash = 'sha512', $bytes = 128)
79: {
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);
102: return $token;
103: }
104: }
105: