pwgen.pl
author Peter Gervai <grin@grin.hu>
Wed, 15 May 2024 09:50:23 +0200
changeset 14 d2d8803e35ec
parent 13 9ec5ecfe4347
permissions -rwxr-xr-x
pwgen.pl: support "hungarian-friendly" speaker mode, basically getting rid of 'qxy' with a given probability

#!/usr/bin/perl
# $Id: pwgen.pl,v 1ad4c2066b77 2020/12/09 14:56:54 grin $
# (c)Peter Gervai, 2002; Released under GPLv2 + CC_BY-SA-2.0
#
# jelszogenerator, 2002/04/08; 2016/11/09; 2020/12/09
#
## This generator mimics Japanese syllable-based language, which
## results easy to pronounce gibberish. 
##
## Puroguramu o go riyo itadaki arigatogozaimasu. :-)
##

use Getopt::Long;

my $help;                       # please?
my $words=5;                    # how many pw to be generated
my $syllab=4;                   # how many syllables in a [pass]word
my $dash;                       # use dashes
my $dashlen=2;                  # use dash after N syllable(s)
my $dashchr='-';                # dash character (if used)
my $longsyl=20;                 # long syllable n% probability
my $nolf=0;                     # no linefeed after the last word (script friendly)
my $hu_friendly = 5;           # n% probability of unfriendly consonants

GetOptions(
    "help" 	    => \&help,
    "words=i" 	=> \$words,
    "syllab=i"	=> \$syllab,
    "dash"	    => \$dash,
    "dashlen=i"	=> \$dashlen,
    "longsyl=i"	=> \$longsyl,
    "dashchr=s"	=> \$dashchr,
    "nolf"	    => \$nolf,
    "hufriendly=i"=> \$hu_friendly,
) or &help;


$vowel='aeiou';
#$cons='bcdfghjklmnpqrstvwxyz'; # consonants without visually confusing ones (like 0 vs o)
 $consh='bcdfghjklmnprstvz';    # non-English user friendly consonants 
 $consx='qxy';                  # for advanced speakers! ;-)


$vlen=length($vowel);
$chlen=length($consh);
$cxlen=length($consx);

for (1..$words) {
    my $dash_count=0;
    my $sylnum=$syllab + rand(3);
    for my $i (1..$sylnum) {
        print &get_consonant;
        print substr($vowel,rand($vlen),1);
        print &get_consonant if $longsyl > rand(100);
        print $dashchr  unless !$dash or ++$dash_count % $dashlen  or  $i>$sylnum-$dashlen;
    }
    print "   " unless $words == 1;
}

print "\n" unless $nolf;


sub help {
    print "Usage:\n";
    print "  $0 [--help] [--words=$words] [--syllab=$syllab] [--longsyl=$longsyl] [--dash [--dashchr=$dashchr] [--dashlen=$dashlen]]\n";
    print "      words:   how many passwords to display (5)\n";
    print "      syllab:  how many syllables in a word (4)\n";
    print "      dash:    use dash between syllables\n";
    print "      dashlen: use dash between every N syllable\n";
    print "      longsyl: long syllable probability (20%)\n";
    print "      dashchr: character to use instead of '-'\n";
    print "      nolf:    don't print a linefeed after the last word\n";
    print "      hufriendly: probability of unfriendly consonants (5%)\n";

    exit;
}

# Hungarian-friendly consonants
sub get_consonant {
    if( $hu_friendly > rand(100) ) {
        # be nasty to native speakers :-)
        return substr($consx, rand($cxlen),1);
    }
    return substr($consh, rand($chlen),1);
}


## syllables: 4 + rnd(3)			- ~1.5 bit
## cons:		rnd(20)				-  4.32 bit (a bit less due to hu_friendly)
## vow:			rnd(5)				-  2.32 bit
## cons2:		rnd(20) * 20%		- ~5.00 bit
##
##									- ~ 13.14 bit/syllable
## 4s: 52.5 b, 5s: 65.7 b, 6s: 78.8 b, 7s: 92 b
##