#!/usr/bin/perl -w # # dicepass # jms1 2013-05-31 # # 2023-03-22 jms1 - support 'infnoise' to choose random numbers # - this requires that the 'infnoise' program be available, that it have the # correct permissions to access the hardware (i.e. setuid root), and that # the hardware be physically plugged into the machine. # - hardware: https://www.crowdsupply.com/leetronics/infinite-noise-trng # - software: https://github.com/waywardgeek/infnoise # # 2023-06-08 jms1 - updated $wordfile to point to keybase public folder require 5.003 ; use strict ; use Getopt::Std ; my %opt = () ; my $wordfile = '/keybase/public/jms1/diceware/words-4d10.txt' ; my $rcount = 0 ; my $icount = 0 ; my %words = () ; # "0000" => "aaaaa" (or whatever it is) ############################################################################### # # usage sub usage(;$) { my $msg = ( shift || '' ) ; print < 0 ? 1 : 0 ) + ( $rcount > 0 ? 1 : 0 ) + ( $ccount > 0 ? 1 : 0 ) ; if ( $nsrc < 1 ) { usage() ; } if ( $nsrc > 1 ) { usage "ERROR: do not mix '-i', '-r', and numbers on the command line\n" ; } ############################################################################### # # Read the wordlist into memory open ( I , '<' , $wordfile ) or die "can't read \"$wordfile\": $!\n" ; while ( my $line = ) { next unless ( $line =~ m|^(\d+)\s+(\S+)| ) ; $words{$1} = $2 ; } close I ; my $nwords = scalar( keys %words ) ; ############################################################################### # # Select words if ( $rcount > 0 ) { ######################################## # Generate pseudo-random words my @wkey = sort keys %words ; while ( $rcount > 0 ) { my $n = int( rand( $nwords ) ) ; my $k = $wkey[$n] ; my $v = $words{$k} ; printf "%s\t%s\n" , $k , $v ; $rcount -- ; } } elsif ( $icount > 0 ) { ######################################## # Generate truly-random words my @wkey = sort keys %words ; while ( $icount > 0 ) { my $n = infnoise( $nwords ) ; my $k = $wkey[$n] ; my $v = $words{$k} ; printf "%s\t%s\n" , $k , $v ; $icount -- ; } } else { ######################################## # Process numbers from the command line while ( my $k = shift @ARGV ) { printf "%s\t%s\n" , $k , ( ( exists $words{$k} ) ? $words{$k} : '(null)' ) ; } }