summaryrefslogtreecommitdiffstats
path: root/lib/Providers/Rng/OpenSSLRNGProvider.php
blob: dc66c64ad308fba08c17d3fa660da15647d40ebb (plain)
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
<?php

namespace RobThree\Auth\Providers\Rng;

class OpenSSLRNGProvider implements IRNGProvider
{
    private $requirestrong;
    
    function __construct($requirestrong = true) {
        $this->requirestrong = $requirestrong;
    }
    
    public function getRandomBytes($bytecount) {
        $result = openssl_random_pseudo_bytes($bytecount, $crypto_strong);
        if ($this->requirestrong && ($crypto_strong === false))
            throw new \RNGException('openssl_random_pseudo_bytes returned non-cryptographically strong value');
        if ($result === false)
            throw new \RNGException('openssl_random_pseudo_bytes returned an invalid value');
        return $result;
    }
    
    public function isCryptographicallySecure() {
        return $this->requirestrong;
    }
}