Conditions | 3 |
Paths | 2 |
Total Lines | 18 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
48 | public static function secure_crypt($min, $max) { |
||
49 | $range = $max - $min; |
||
50 | |||
51 | if ($range < 0) { |
||
52 | return $min; // not so random... |
||
53 | } |
||
54 | |||
55 | $log = log($range, 2); |
||
56 | $bytes = (int) ($log / 8) + 1; // length in bytes |
||
57 | $bits = (int) $log + 1; // length in bits |
||
58 | $filter = (int) (1 << $bits) - 1; // set all lower bits to 1 |
||
59 | do { |
||
60 | $rnd = hexdec( bin2hex( openssl_random_pseudo_bytes( $bytes ) ) ); |
||
61 | $rnd = $rnd & $filter; // discard irrelevant bits |
||
62 | } while ($rnd >= $range); |
||
63 | |||
64 | return $min + $rnd; |
||
65 | } |
||
66 | |||
79 |