Perlで強度の高いパスワードをランダムに作成するプログラムを書いてみた
娘に 「強度の高いパスワードを作ってよ」 と言われたので、ランダムにパスワードを作成するプログラムを Perl で書いてみました。
パスワード生成の仕様としては、アルファベットの大文字と小文字と数字と記号を含む、8文字のランダムな文字列を作成します。
#!/usr/bin/perl use strict; use warnings; my @str1 = qw(a b c d e f g h i j k m n o p q r s t u v w x y z); my @str2 = qw(A B C D E F G H J K L M N P Q R S T U V W X Y Z); my @str3 = qw(1 2 3 4 5 6 7 8 9); my @str4 = qw(+ - / = _); my @pattern = (); my $historyP = ""; my $cnt = 0; while($cnt < 4){ my $type = int(rand()*4); if($historyP =~ m/$type /){ next; }else{ $historyP .= $type." "; push(@pattern, $type); $cnt++; } } my $history1 = ""; my $history2 = ""; my $history3 = ""; my $history4 = ""; my $pass = ""; $cnt = 0; while(length($pass) < 8){ my $type = ""; if($cnt < 4){ $type = shift(@pattern); }else{ $type = int(rand()*4); } if($type == 0){ my ($idx, $word) = SelectWord(25, @str1); if($history1 =~ m/$idx /){ next; }else{ $history1 .= $idx." "; $pass .= $word; $cnt++; } }elsif($type == 1){ my ($idx, $word) = SelectWord(24, @str2); if($history2 =~ m/$idx /){ next; }else{ $history2 .= $idx." "; $pass .= $word; $cnt++; } }elsif($type == 2){ my ($idx, $word) = SelectWord(9, @str3); if($history3 =~ m/$idx /){ next; }else{ $history3 .= $idx." "; $pass .= $word; $cnt++; } }elsif($type == 3){ my ($idx, $word) = SelectWord(5, @str4); if($history4 =~ m/$idx /){ next; }else{ $history4 .= $idx." "; $pass .= $word; $cnt++; } }else{ print "out of index $type\n"; } } print "$pass\n"; exit; sub SelectWord{ my $len = shift@_; my @words = @_; my $idx = int(rand()*$len); my $word = $words[$idx]; return($idx, $word); }
Enjoy!