3 require_once "LoomRandom.php";
4 require_once "bcbitwise.php";
7 * Roll dice to generate random passphrases.
8 * From http://world.std.com/%7Ereinhold/diceware.wordlist.asc
18 function Diceware($random=false) {
19 if (!$random) $random = new LoomRandom();
20 $text = $this->rawtext();
21 $words = explode("\n", $text);
22 $bytes = 32; // 2**128 should be big enough, doncha think?
24 $this->random
= $random;
25 $this->count
= count($words);
26 $this->words
= $words;
27 $this->bytes
= $bytes;
30 function random_word() {
31 $random = $this->random
;
32 $bytes = $this->bytes
;
33 $count = $this->count
;
34 $bits = $random->urandom_bytes($bytes);
35 $words = $this->words
;
37 $x = bchexdec(bin2hex($bits));
38 $wordnum = bcdiv(bcmul($x, $count), bcpow(2, 8 * $bytes), 0);
39 return $words[$wordnum];
42 function random_words($count) {
44 for ($i=0; $i<$count; $i++
) {
45 if ($res != "") $res .= " ";
46 $res .= $this->random_word();
52 // This is the official Diceware word list, with the dice numbers removed,
53 // and a backslash escaping the double-quote.
7833 } // end of Diceware class
7837 $diceware = new Diceware();
7838 echo $diceware->random_words(5) . "\n";
7842 // Copyright 2008 Bill St. Clair
7844 // Licensed under the Apache License, Version 2.0 (the "License");
7845 // you may not use this file except in compliance with the License.
7846 // You may obtain a copy of the License at
7848 // http://www.apache.org/licenses/LICENSE-2.0
7850 // Unless required by applicable law or agreed to in writing, software
7851 // distributed under the License is distributed on an "AS IS" BASIS,
7852 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7853 // See the License for the specific language governing permissions
7854 // and limitations under the License.