3 require_once "LoomRandom.php";
6 * Roll dice to generate random passphrases.
7 * From http://world.std.com/%7Ereinhold/diceware.wordlist.asc
17 function Diceware($random=false) {
18 if (!$random) $random = new LoomRandom();
19 $text = $this->rawtext();
20 $words = explode("\n", $text);
21 // Assume < 2**16 words in list.
22 // Check for arithmetic overflow in random_word if you increase this
25 $this->random
= $random;
26 $this->count
= count($words);
27 $this->words
= $words;
28 $this->bytes
= $bytes;
31 function random_word() {
32 $random = $this->random
;
33 $bytes = $this->bytes
;
34 $count = $this->count
;
35 $bits = $random->urandom_bytes($bytes);
36 $words = $this->words
;
38 // There must be an easier way to do this
39 $x = base_convert(bin2hex($bits), 16, 10);
40 return $words[floor(($x * $count) / pow(2, 8 * $bytes))];
43 function random_words($count) {
45 for ($i=0; $i<$count; $i++
) {
46 if ($res != "") $res .= " ";
47 $res .= $this->random_word();
53 // This is the official Diceware word list, with the dice numbers removed,
54 // and a backslash escaping the double-quote.
7834 } // end of Diceware class
7838 $diceware = new Diceware();
7839 echo $diceware->random_words(5) . "\n";
7843 // Copyright 2008 Bill St. Clair
7845 // Licensed under the Apache License, Version 2.0 (the "License");
7846 // you may not use this file except in compliance with the License.
7847 // You may obtain a copy of the License at
7849 // http://www.apache.org/licenses/LICENSE-2.0
7851 // Unless required by applicable law or agreed to in writing, software
7852 // distributed under the License is distributed on an "AS IS" BASIS,
7853 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7854 // See the License for the specific language governing permissions
7855 // and limitations under the License.