-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPassPhrase.php
More file actions
44 lines (34 loc) · 1.16 KB
/
PassPhrase.php
File metadata and controls
44 lines (34 loc) · 1.16 KB
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
<?php
namespace App\Utility;
use Illuminate\Support\Facades\Cache;
use ReflectionClass;
use Illuminate\Support\Str;
class PassPhrase
{
/**
* returns a phrase containing a random number of words from the wordlist using a separator string
*
* @param int count of the number of words required
* @param string seperator string
* @return string
*/
public function passPhrase(int $count, string $separator = '-') :string
{
$phraseWords = collect();
$words = $this->wordsArray();
$max = count($words)-1; //max index
foreach(range(1,$count) as $iter) {
$phraseWords->push($words[random_int(0,$max)]);
}
return $phraseWords->implode($separator);
}
private function wordsArray()
{
return Cache::rememberForever('passphrase-words', function () {
//assumes wordlist is in the same folder as this class.
$reflector = new ReflectionClass(Self::class);
$filepath = Str::beforeLast($reflector->getFileName(),'/');
return explode("\n", file_get_contents($filepath . '/wordlist.txt'));
});
}
}