当前位置: 代码网 > it编程>网页制作>Perl > 使用Perl生成随机密码

使用Perl生成随机密码

2024年05月15日 Perl 我要评论
可以通过参数控制生成密码中包括的字符种类#!/usr/bin/perluse strict;use warnings;use getopt::std;sub show_help { print "u

可以通过参数控制生成密码中包括的字符种类

#!/usr/bin/perl

use strict;
use warnings;
use getopt::std;

sub show_help {
  print "useage:\n";
  print "newp -aansl\n";
  print "-a\t\t the password contains lower case letters(a-z)\n";
  print "-a\t\t the password contains upper case letters(a-z)\n";
  print "-n\t\t the password contains numerical character(0-9)\n";
  print "-s\t\t the password contains special symbols\n";
  print "-u\t\t the password contains only unique characters\n";
  print "-l length\t set the password length(default: 6)\n";

  exit 0;
}

sub show_version {
  print "version: 0.2.1 changed the default option: -l 9 -ana. 2016-4-15\n";

  exit 0;
}

### main program

use vars qw($opt_a $opt_a $opt_h $opt_l $opt_n $opt_s $opt_u $opt_v);
getopts('aahl:nsuv');

&show_version if $opt_v;
&show_help if $opt_h;

my $len = $opt_l || 9;  # default length 9
my $opt_cnt = 0;
my @rand_str = ();

# store all the characters
my @num = qw(0 1 2 3 4 5 6 7 8 9);
my @abc = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z);
my @abc = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z);
# my @sym = qw(! " $ % & ' * + - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~);
my @sym = qw(! $ % & * + - . / : ; < = > ? @ [ ] ^ _ ` { | } ~); # no " ' \
unshift (@sym, '(', ')', '#', ','); # to prevent perl's complains or warnings.
my @all_sym = (@num, @abc, @abc, @sym);
my @ch_src = ();

if ((!$opt_a) && (!$opt_a) && (!$opt_n) && (!$opt_s)) {
  $opt_a++;
  $opt_a++;
  $opt_n++;
}

if ($opt_a) {
  $opt_cnt++;
  my $i = rand @abc;
  unshift @rand_str, $abc[$i];

  if ($opt_u) {
    if ($i>=1) {
      $abc[$i-1] = shift @abc;
    } else {
      shift @abc;
    }
  }

  unshift (@ch_src, @abc);
}

if ($opt_a) {
  $opt_cnt++;
  my $i = rand @abc;
  unshift @rand_str, $abc[$i];

  if ($opt_u) {
    if ($i>=1) {
      $abc[$i-1] = shift @abc;
    } else {
      shift @abc;
    }
  }

  unshift (@ch_src, @abc);
}

if ($opt_n) {
  $opt_cnt++;
  my $i = rand @num;
  unshift @rand_str, $num[$i];

  if ($opt_u) {
    if ($i>=1) {
      $num[$i-1] = shift @num;
    } else {
      shift @num;
    }
  }

  unshift (@ch_src, @num);
}

if ($opt_s) {
  $opt_cnt++;
  my $i = rand @sym;
  unshift @rand_str, $sym[$i];

  if ($opt_u) {
    if ($i>=1) {
      $sym[$i-1] = shift @sym;
    } else {
      shift @sym;
    }
  }

  unshift (@ch_src, @sym);
}

if ($len < $opt_cnt) {
  print "the count of characters[$len] should not be smaller " .
     "than count of character types[$opt_cnt].\n";
  exit -1;
}

if ($opt_u && $len > (@ch_src + @rand_str)) {
  print "the total number of characters[".(@ch_src + @rand_str).
     "] which could be contained " .
     "in password is smaller than the length[$len] of it.\n";
  exit -1;
}

foreach (1..$len-$opt_cnt) {
  my $i = rand @ch_src;
  unshift @rand_str, $ch_src[$i];

  if ($opt_u) {
    if ($i>=1) {
      $ch_src[$i-1] = shift @ch_src;
    } else {
      shift @ch_src;
    }
  }
}

foreach (1..$len) {
  my $i = rand @rand_str;
  print $rand_str[$i];

  if ($i>=1) {
    $rand_str[$i-1] = shift @rand_str;
  } else {
    shift @rand_str;
  }
}

print "\n";
exit 0;

以上就是本文给大家分享的全部代码了,希望对大家学习perl能够有所帮助

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com