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 Xoops\Core;
13:
14: use RandomLib\Factory;
15:
16: /**
17: * XOOPS Random generator
18: *
19: * @category Xoops\Core
20: * @package Random
21: * @author Richard Griffith <richard@geekwright.com>
22: * @copyright 2015 XOOPS Project (http://xoops.org)
23: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24: * @version Release: 1.0
25: * @link http://xoops.org
26: * @since 2.0.0
27: */
28: class Random
29: {
30: /**
31: * Create a one time token
32: *
33: * Generates a low strength random number of size $bytes and hash with the
34: * algorithm specified in $hash.
35: *
36: * @param string $hash hash function to use
37: * @param integer $bytes the number of random bit to generate
38: *
39: * @return string hashed token
40: */
41: public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
42: {
43: $factory = new Factory;
44: $generator = $factory->getLowStrengthGenerator();
45: $token = hash($hash, $generator->generate($bytes));
46: return $token;
47: }
48:
49: /**
50: * Create a medium strength key
51: *
52: * Generates a medium strength random number of size $bytes and hash with the
53: * algorithm specified in $hash.
54: *
55: * @param string $hash hash function to use
56: * @param integer $bytes the number of random bytes to generate
57: *
58: * @return string hashed token
59: */
60: public static function generateKey($hash = 'sha512', $bytes = 128)
61: {
62: $factory = new Factory;
63: $generator = $factory->getMediumStrengthGenerator();
64: $token = hash($hash, $generator->generate($bytes));
65: return $token;
66: }
67: }
68: