Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1.     public function testDiceware($numWords = 3)
  2.     {
  3.         $wordlistUrl = 'https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt';
  4.  
  5.         if (!file_exists(CACHE_DIR . '/' . basename($wordlistUrl))) {
  6.             $wordlist = file_get_contents($wordlistUrl);
  7.             file_put_contents(CACHE_DIR . '/' . basename($wordlistUrl), $wordlist);
  8.         } else {
  9.             $wordlist = file_get_contents(CACHE_DIR . '/' . basename($wordlistUrl));
  10.         }
  11.  
  12.         $dies = 5;
  13.         $words = [];
  14.  
  15.         for ($n = 0; $n < $numWords; $n++) {
  16.  
  17.             $rolls = [];
  18.  
  19.             for ($i = $dies; $i > 0; $i--) {
  20.                 $rolls[] = rand(1, 6);
  21.             }
  22.  
  23.             $key = implode('', $rolls);
  24.  
  25.             preg_match('#^' . $key . '\s+(.*?)$#sm', $wordlist, $matches);
  26.             $words[] = $matches[1];
  27.         }
  28.  
  29.         $phrase = str_replace(' ', '', lcfirst(ucwords(implode(' ', $words))));
  30.  
  31.         return $phrase;
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement