Skip to content

Instantly share code, notes, and snippets.

@mtsukamoto
Created August 30, 2011 04:45
Show Gist options
  • Save mtsukamoto/1180203 to your computer and use it in GitHub Desktop.
Save mtsukamoto/1180203 to your computer and use it in GitHub Desktop.
Minimal random password generater.
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;
}
@mtsukamoto
Copy link
Author

「少なくとも4種類入ったパスワードを作成 - Perl日記」を見て作成。僕もコメントと同意見で、各文字種で1文字ずつ作った後に残りを生成して、ランダムに並べ変えるロジックのほうがredoが無くてイイと思う。
http://d.hatena.ne.jp/rightgo09/20110514/p1#c1306051871

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment