/var/www/hkosl.com/b2b2c/webadmin/libraies/aura/session/src/Randval.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
 *
 * This file is part of Aura for PHP.
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 */
namespace Aura\Session;

use 
Aura\Session\Exception;

/**
 *
 * Generates cryptographically-secure random values.
 *
 * @package Aura.Session
 *
 */
class Randval implements RandvalInterface
{
    
/**
     *
     * An object to intercept PHP function calls; this makes testing easier.
     *
     * @var Phpfunc
     *
     */
    
protected $phpfunc;

    
/**
     *
     * Constructor.
     *
     * @param Phpfunc $phpfunc An object to intercept PHP function calls;
     * this makes testing easier.
     *
     */
    
public function __construct(Phpfunc $phpfunc)
    {
        
$this->phpfunc $phpfunc;
    }

    
/**
     *
     * Returns a cryptographically secure random value.
     *
     * @return string
     *
     * @throws Exception if neither openssl nor mcrypt is available.
     *
     */
    
public function generate()
    {
        
$bytes 32;

        if (
$this->phpfunc->function_exists('random_bytes')) {
            return 
$this->phpfunc->random_bytes($bytes);
        }

        if (
$this->phpfunc->extension_loaded('openssl')) {
            return 
$this->phpfunc->openssl_random_pseudo_bytes($bytes);
        }

        if (
$this->phpfunc->extension_loaded('mcrypt')) {
            return 
$this->phpfunc->mcrypt_create_iv($bytesMCRYPT_DEV_URANDOM);
        }

        
$message "Cannot generate cryptographically secure random values. "
                 
"Please install extension 'openssl' or 'mcrypt', or use "
                 
"another cryptographically secure implementation.";

        throw new 
Exception($message);
    }
}