使用Perl生成随机密码

 更新时间:2016年06月19日 15:22:24   作者:Shimmey  
本文给大家分享的是使用perl生成随机密码的方法示例,而且可以通过参数来控制密码中包含的字符种类,非常的不错,有需要的小伙伴可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

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

#!/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能够有所帮助

相关文章

  • Perl中使用MIME::Lite发送邮件实例

    Perl中使用MIME::Lite发送邮件实例

    这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下
    2014-09-09
  • Perl中的真与假深入研究

    Perl中的真与假深入研究

    这篇文章主要介绍了Perl中的真与假深入研究,本文详细讲解了Perl中真值与假值的不同,需要的朋友可以参考下
    2015-06-06
  • perl 删除数组元素的几种方法小结

    perl 删除数组元素的几种方法小结

    perl 删除数组元素的几种方法,有需要的朋友可以参考下
    2013-02-02
  • 求婚示爱的Perl代码之注释篇

    求婚示爱的Perl代码之注释篇

    Deparse 模块是一个很好的打印机,它可以揭开 Perl 代码神秘的面纱,帮助你理解优化器为你的代码做了那些转换。换言之,它会重新生成Perl代码,试着略去一些晦涩难懂的部分,把代码用一致的格式写出来
    2013-03-03
  • Perl List::Util模块使用实例

    Perl List::Util模块使用实例

    这篇文章主要介绍了Perl List::Util模块使用实例,本文给出扫描符合条件的某个列表并取出第一个符合条件的、求1到1000之间的和 、求一组数字的最大值与最小值等实用功能代码,需要的朋友可以参考下
    2015-06-06
  • Perl学习基本备忘录

    Perl学习基本备忘录

    Perl学习基本备忘录,包括perl数组、命令行执行perl语句、perl引号规则、变量初始化、perl特殊变量等,有需要的朋友,可以参考下
    2013-02-02
  • perl控制流介绍(if条件,while,for循环,foreach)

    perl控制流介绍(if条件,while,for循环,foreach)

    Perl控制流(if条件,while,for循环),需要的朋友可以参考下
    2013-02-02
  • 求婚示爱的Perl代码之改写篇

    求婚示爱的Perl代码之改写篇

    求婚示爱的Perl代码之改写篇,特分享下, 方便需要的朋友
    2013-03-03
  • perl文件操作的一些例子分享

    perl文件操作的一些例子分享

    有关perl文件操作的一些例子,供大家学习参考
    2013-02-02
  • perl读写文件代码实例

    perl读写文件代码实例

    这篇文章主要介绍了perl读写文件代码实例,本文直接给出实现代码,需要的朋友可以参考下
    2015-05-05

最新评论

?


http://www.vxiaotou.com