Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

Random::generateKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 7
loc 7
rs 9.4285
ccs 5
cts 5
cp 1
cc 1
eloc 5
nc 1
nop 2
crap 1
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
use RandomLib\Factory;
15
16
/**
17
 * XOOPS Random generator
18
 *
19
 * @category  Xmf\Random
20
 * @package   Xmf
21
 * @author    Richard Griffith <[email protected]>
22
 * @copyright 2015-2016 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
 */
27
class Random
28
{
29
    /**
30
     * Create a one time token
31
     *
32
     * Generates a low strength random number of size $bytes and hash with the
33
     * algorithm specified in $hash.
34
     *
35
     * @param string  $hash  hash function to use
36
     * @param integer $bytes the number of random bit to generate
37
     *
38
     * @return string hashed token
39
     */
40 9 View Code Duplication
    public static function generateOneTimeToken($hash = 'sha512', $bytes = 64)
41
    {
42 9
        $factory = new Factory;
43 9
        $generator = $factory->getLowStrengthGenerator();
44 9
        $token = hash($hash, $generator->generate($bytes));
45 9
        return $token;
46
    }
47
48
    /**
49
     * Create a medium strength key
50
     *
51
     * Generates a medium strength random number of size $bytes and hash with the
52
     * algorithm specified in $hash.
53
     *
54
     * @param string  $hash  hash function to use
55
     * @param integer $bytes the number of random bytes to generate
56
     *
57
     * @return string hashed token
58
     */
59 1 View Code Duplication
    public static function generateKey($hash = 'sha512', $bytes = 128)
60
    {
61 1
        $factory = new Factory;
62 1
        $generator = $factory->getMediumStrengthGenerator();
63 1
        $token = hash($hash, $generator->generate($bytes));
64 1
        return $token;
65
    }
66
}
67