pwgen.pl
author Peter Gervai <grin@grin.hu>
Wed, 15 May 2024 09:04:25 +0200
changeset 13 9ec5ecfe4347
parent 5 98bc7c6e581a
child 14 d2d8803e35ec
permissions -rwxr-xr-x
pwgen: add comment about entropy calculation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     1
#!/usr/bin/perl
5
98bc7c6e581a pwgen.pl: option to skip LF at the end of the line
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     2
# $Id: pwgen.pl,v 1ad4c2066b77 2020/12/09 14:56:54 grin $
98bc7c6e581a pwgen.pl: option to skip LF at the end of the line
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     3
# (c)Peter Gervai, 2002; Released under GPLv2 + CC_BY-SA-2.0
0
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     4
#
5
98bc7c6e581a pwgen.pl: option to skip LF at the end of the line
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     5
# jelszogenerator, 2002/04/08; 2016/11/09; 2020/12/09
0
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     6
#
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     7
3
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
     8
use Getopt::Long;
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
     9
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    10
my $help;
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    11
my $words=5;
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    12
my $syllab=4;
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    13
my $dash;
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    14
my $dashlen=2;
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    15
my $dashchr='-';
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    16
my $longsyl=20;
5
98bc7c6e581a pwgen.pl: option to skip LF at the end of the line
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    17
my $nolf=0;
3
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    18
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    19
GetOptions(
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    20
  "help" 	=> \&help,
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    21
  "words=i" 	=> \$words,
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    22
  "syllab=i"	=> \$syllab,
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    23
  "dash"	=> \$dash,
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    24
  "dashlen=i"	=> \$dashlen,
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    25
  "longsyl=i"	=> \$longsyl,
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    26
  "dashchr=s"	=> \$dashchr,
5
98bc7c6e581a pwgen.pl: option to skip LF at the end of the line
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    27
  "nolf"	=> \$nolf,
3
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    28
) or &help;
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    29
0
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
$vowel='aeiou';
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    32
#$cons='bcdfghjklmnpqrstvwxyz';
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
 $cons='bcdfghjklmnprstvxyz';
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
$vlen=length($vowel);
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
$clen=length($cons);
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
$longsyl=20; # percent
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    39
3
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    40
for (1..$words) {
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    41
    my $dash_count=0;
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    42
    my $sylnum=$syllab + rand(3);
0
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
    for my $i (1..$sylnum) {
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
        print substr($cons,rand($clen),1);
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
        print substr($vowel,rand($vlen),1);
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
        print substr($cons,rand($clen),1) if $longsyl > rand(100);
3
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    47
        print $dashchr  unless !$dash or ++$dash_count % $dashlen  or  $i>$sylnum-$dashlen;
0
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
    }
5
98bc7c6e581a pwgen.pl: option to skip LF at the end of the line
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    49
    print "   " unless $words == 1;
0
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    50
}
624a9ab34425 Add code lying around to the repo
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    51
5
98bc7c6e581a pwgen.pl: option to skip LF at the end of the line
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    52
print "\n" unless $nolf;
3
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    53
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    54
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    55
sub help {
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    56
    print "Usage:\n";
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    57
    print "  $0 [--help] [--words=$words] [--syllab=$syllab] [--longsyl=$longsyl] [--dash [--dashchr=$dashchr] [--dashlen=$dashlen]]\n";
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    58
    print "      words:   how many passwords to display\n";
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    59
    print "      syllab:  how many syllables in a word\n";
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    60
    print "      dash:    use dash between syllables\n";
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    61
    print "      dashlen: use dash between every N syllable\n";
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    62
    print "      longsyl: long syllable probability (percent)\n";
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    63
    print "      dashchr: character to use instead of '-'\n";
5
98bc7c6e581a pwgen.pl: option to skip LF at the end of the line
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    64
    print "      nolf:    don't print a linefeed after the last word\n";
3
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    65
    exit;
d4bb4405d439 pwgen.pl: Implement proper getopt, and install various knobs and wheels to
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    66
}
13
9ec5ecfe4347 pwgen: add comment about entropy calculation
Peter Gervai <grin@grin.hu>
parents: 5
diff changeset
    67
9ec5ecfe4347 pwgen: add comment about entropy calculation
Peter Gervai <grin@grin.hu>
parents: 5
diff changeset
    68
9ec5ecfe4347 pwgen: add comment about entropy calculation
Peter Gervai <grin@grin.hu>
parents: 5
diff changeset
    69
## syllables: 4 + rnd(3)			- ~1.5 bit
9ec5ecfe4347 pwgen: add comment about entropy calculation
Peter Gervai <grin@grin.hu>
parents: 5
diff changeset
    70
## cons:		rnd(20)				-  4.32 bit
9ec5ecfe4347 pwgen: add comment about entropy calculation
Peter Gervai <grin@grin.hu>
parents: 5
diff changeset
    71
## vow:			rnd(5)				-  2.32 bit
9ec5ecfe4347 pwgen: add comment about entropy calculation
Peter Gervai <grin@grin.hu>
parents: 5
diff changeset
    72
## cons2:		rnd(20) * 20%		- ~5.00 bit
9ec5ecfe4347 pwgen: add comment about entropy calculation
Peter Gervai <grin@grin.hu>
parents: 5
diff changeset
    73
##
9ec5ecfe4347 pwgen: add comment about entropy calculation
Peter Gervai <grin@grin.hu>
parents: 5
diff changeset
    74
##									- ~ 13.14 bit/syllable
9ec5ecfe4347 pwgen: add comment about entropy calculation
Peter Gervai <grin@grin.hu>
parents: 5
diff changeset
    75
## 4s: 52.5 b, 5s: 65.7 b, 6s: 78.8 b, 7s: 92 b
9ec5ecfe4347 pwgen: add comment about entropy calculation
Peter Gervai <grin@grin.hu>
parents: 5
diff changeset
    76
##