使用Perl生成随机密码

所属分类: 脚本专栏 / perl 阅读数: 1138
收藏 0 赞 0 分享

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

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

更多精彩内容其他人还在看

使用 use re debug 查看正则表达式的匹配过程

使用 use re 'debug' 查看正则表达式的匹配过程,参见如下的代码
收藏 0 赞 0 分享

perl中的$a和$b介绍

有关perl中的$a和$b,这两个变量是为sort函数准备的内置变量,所以声明时可以不加 my
收藏 0 赞 0 分享

perl用{}修饰变量名的写法分享

在perl中用{}修饰变量名,可以防止 _ 被解释为变量名的一部分
收藏 0 赞 0 分享

Perl使用File::Basename获取文件扩展名的代码

本文为大家介绍的这个例子,实现了获取/home/topgkw中所有文件后缀,其中目录返回空值
收藏 0 赞 0 分享

Perl 哈希Hash用法之入门教程

本文和大家重点讨论一下Perl Hash的用法,哈希是一种数据结构,和数组类似,但是,和数组不同的是,其索引不是数字,而是名字。也就是说,索引(这里,我们将它叫key)不是数字而是任意的唯一的字符串
收藏 0 赞 0 分享

perl哈希的一个实例分析

上一篇文章介绍了hash的入门教程,这篇文章为大家提供一个实例,方便大家深入学习
收藏 0 赞 0 分享

Perl哈希表用法解析

Perl语言有很多值得学习的地方,那么你对Perl哈希表的概念是否熟悉呢,这里和大家分享一下,希望本文的介绍能让你有所收获
收藏 0 赞 0 分享

Perl 哈希的创建和引用介绍

创建,引用仅有两种方法,使用它也是两种,这里简单介绍下, 方便需要的朋友
收藏 0 赞 0 分享

Perl 函数集小结

perl中常用的函数集合,特分享下,方便需要的朋友
收藏 0 赞 0 分享

perl的POD权限问题处理

今天我们继续查找mod_perl对req_header的处理,有需要的朋友建议参考学习之
收藏 0 赞 0 分享
查看更多