スポンサーリンク

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!

スポンサーリンク
コメント
Verify your Comment
Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

コメントを投稿
トラックバック

このページのトラックバックURL:
https://www.typepad.com/services/trackback/6a01310f1f31c0970c017d42e7f3b4970c

このページへのトラックバック一覧 Perlで強度の高いパスワードをランダムに作成するプログラムを書いてみた:


プロフィール


  • 書いてる人:夢界 陸

    名古屋在住のおっさん。
    プログラミングやガジェットの話など、 日々の興味を徒然と綴っています。



    Twitterやってます @mukairiku

    運営サイト
    www.mukairiku.net

ブログ内検索

Licenses

  • Creative Commons License

OTHER

  • このブログのはてなブックマーク数

Blog powered by TypePad

スポンサーリンク