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-2016 XOOPS Project (http://xoops.org)
21: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22: * @link http://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: */
37: public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
38: {
39: $token = hash($hash, random_bytes($bytes));
40: return $token;
41: }
42:
43: /**
44: * Create a medium strength key
45: *
46: * Generates a medium strength random number of size $bytes and hash with the
47: * algorithm specified in $hash.
48: *
49: * @param string $hash hash function to use
50: * @param integer $bytes the number of random bytes to generate
51: *
52: * @return string hashed token
53: */
54: public static function generateKey($hash = 'sha512', $bytes = 128)
55: {
56: $token = hash($hash, random_bytes($bytes));
57: return $token;
58: }
59: }
60: