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 /* ***** BEGIN LICENSE BLOCK *****
7843 * Version: MPL 1.1/GPL 2.0/LGPL 2.1/Apache 2.0
7845 * The contents of this file are subject to the Mozilla Public License Version
7846 * 1.1 (the "License"); you may not use this file except in compliance with
7847 * the License. You may obtain a copy of the License at
7848 * http://www.mozilla.org/MPL/
7850 * Software distributed under the License is distributed on an "AS IS" basis,
7851 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7852 * for the specific language governing rights and limitations under the
7855 * The Original Code is LoomClient PHP library
7857 * The Initial Developer of the Original Code is
7859 * Portions created by the Initial Developer are Copyright (C) 2008
7860 * the Initial Developer. All Rights Reserved.
7863 * Bill St. Clair <bill@billstclair.com>
7865 * Alternatively, the contents of this file may be used under the
7866 * terms of the GNU General Public License Version 2 or later (the
7867 * "GPL"), the GNU Lesser General Public License Version 2.1 or later
7868 * (the "LGPL"), or The Apache License Version 2.0 (the "AL"), in
7869 * which case the provisions of the GPL, LGPL, or AL are applicable
7870 * instead of those above. If you wish to allow use of your version of
7871 * this file only under the terms of the GPL, the LGPL, or the AL, and
7872 * not to allow others to use your version of this file under the
7873 * terms of the MPL, indicate your decision by deleting the provisions
7874 * above and replace them with the notice and other provisions
7875 * required by the GPL or the LGPL. If you do not delete the
7876 * provisions above, a recipient may use your version of this file
7877 * under the terms of any one of the MPL, the GPL the LGPL, or the AL.
7878 ****** END LICENSE BLOCK ***** */