ugly_password.pl
| #!/usr/bin/env perl | |
| # Copyright 2018-2026 Peter Bailie | |
| # SPDX-License-Identifier: MIT | |
| # Full license text: https://opensource.org/licenses/MIT | |
| # I created this script for generating ugly passwords for my development lab. | |
| # Ugly passwords will be randomly generated to STDOUT using four character classes. | |
| # Passwords are generated with /dev/urandom to encourage reliable pseudo-random entropy. | |
| # THIS SCRIPT HAS NOT BEEN AUDITED FOR SECURITY. USE AT YOUR OWN RISK. | |
| # Usage: | |
| # $ ./ugly_password.pl [length] | |
| # Where [length] is the ugly password length. Default is 32. | |
| use strict; | |
| use warnings; | |
| use autodie; | |
| my $DEFAULT_LENGTH = 32; | |
| my @PW_CHARS = split //, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*-_+?'; | |
| my ($fh, $read, $byte, $i, $pw_length, $pool_length, $mod_unbias, $password); | |
| $pw_length = defined $ARGV[0] && $ARGV[0] =~ /^\d+$/ ? $ARGV[0] : $DEFAULT_LENGTH; | |
| $pool_length = scalar @PW_CHARS; | |
| $mod_unbias = 255 - (256 % $pool_length); | |
| $password = ""; | |
| open $fh, "<:raw", "/dev/urandom"; | |
| for (1..$pw_length) { | |
| do { | |
| die "Unexpected EOF reading /dev/urandom\n" if (read $fh, $byte, 1) == 0; | |
| $byte = unpack "C", $byte; | |
| } while ($byte > $mod_unbias); | |
| $i = $byte % $pool_length; | |
| $password .= $PW_CHARS[$i]; | |
| } | |
| close $fh; | |
| print STDOUT "${password}\n"; | |
| exit 0; | |
| # EOF |
评论
?
参与讨论