Created
August 30, 2011 04:45
-
-
Save mtsukamoto/1180203 to your computer and use it in GitHub Desktop.
Minimal random password generater.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use utf8; | |
use strict; | |
use warnings; | |
print &generate_password(@ARGV); | |
sub generate_password { | |
my $length = shift || 8; | |
my $seeds = shift || [['a'..'z'],['A'..'Z'],[0..9],[qw(! $ % & @ ? * + -),'#']]; | |
# generate random characters array | |
my @chars = (); | |
foreach my $seed (@$seeds) { | |
push(@chars, $seed->[int(rand(scalar @$seed))]); | |
} | |
while (scalar @chars < $length) { | |
my $seed = $seeds->[int(rand(scalar @$seeds))]; | |
push(@chars, $seed->[int(rand(scalar @$seed))]); | |
} | |
# concat with random order | |
my $password = ''; | |
$password .= splice(@chars, int(rand(scalar @chars)), 1) while (@chars); | |
$password = substr($password, 0, $length); | |
return $password; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
「少なくとも4種類入ったパスワードを作成 - Perl日記」を見て作成。僕もコメントと同意見で、各文字種で1文字ずつ作った後に残りを生成して、ランダムに並べ変えるロジックのほうがredoが無くてイイと思う。
http://d.hatena.ne.jp/rightgo09/20110514/p1#c1306051871