1: <?php
2:
3: namespace PHPixie\Security;
4:
5: class Random
6: {
7: protected $characters;
8:
9: public function string($length)
10: {
11: $characters = $this->characters();
12:
13: $string = '';
14: $max = count($characters) - 1;
15:
16: for ($i = 0; $i < $length; $i++) {
17: $string.= $characters[random_int(0, $max)];
18: }
19:
20: return $string;
21: }
22:
23: public function bytes($length)
24: {
25: return random_bytes($length);
26: }
27:
28: protected function characters()
29: {
30: if($this->characters === null) {
31: $this->characters = $this->getCharacters();
32: }
33:
34: return $this->characters;
35: }
36:
37: protected function getCharacters()
38: {
39: return str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
40: }
41: }
42: