使用Perl生成随机密码

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

#!/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)

相关推荐

  • perl产生随机数实现代码

    Perl利用函数rand()和srand()为随机数(更确切的说是"伪随机数")字符串的生成提供了基本的工具.这些函数不是利用加密来提供安 全性的,所以不要利用它们为你金融信息的加密.但是,如果你需要为你的下一个游戏或者动态Web网站的新特性设计一个简单的随机数生成器,那么 rand()和srand()可能就是你所需要的. 函数rand()是真正的随机数生成器,而srand()会设置供rand()使用的随机数种子.函数rand()会返回一个处于0和你所指定的数 值(缺省为1)之间的分数

  • perl生成特定碱基比例的随机序列的代码

    方法一(不使用模块,by agonyr) 复制代码 代码如下: #!/usr/bin/perl -w use strict; my @seq = ( "A", "T", "C", "G" );my $length = 10000; undef my %hash;$hash{"A"} = int( $length * 0.3 );$hash{"C"} = int( $length * 0.

  • perl写的一个随机编故事的程序(rand随机函数)

    复制代码 代码如下: #!/bin/perluse strict;  use warnings; #定义变量  my $count;  my $input;  my $number;  my $sentence;  my $story; #定义四个数组  #定义了人物数组  my @nouns=  (     'Dad',     'TV',     'Mom',     'Groucho',     'Rebecca',     'Harpo',     'Robin Hood',     '

  • perl 指定长度并生成一个随机的DNA序列的脚本代码

    复制代码 代码如下: #!/bin/perl use strict;  use warnings; #进行定义  my @dna;  my $dna_length;  my $newbase;  my $i=0; print "please input the DNA length\n";  chomp($dna_length=<>); while($i<$dna_length)  {    #从四个碱基中随机选取一个    my(@nucleotides)=qw/A

  • 使用Perl生成随机密码

    可以通过参数控制生成密码中包括的字符种类 #!/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

  • Shell脚本生成随机密码的若干种可能

    1.生成随机密码(urandom版本) #!/bin/bash #Author:丁丁历险(Jacob) #/dev/urandom文件是Linux内置的随机设备文件 #cat /dev/urandom可以看看里面的内容,ctrl+c退出查看 #查看该文件内容后,发现内容有些太随机,包括很多特殊符号,我们需要的密码不希望使用这些符号 #tr -dc '_A-Za-z0-9' </dev/urandom #该命令可以将随机文件中其他的字符删除,仅保留大小写字母,数字,下划线,但是内容还是太多 #我们

  • 使用Python生成随机密码的示例分享

    生成随机密码这件事情用python来干确实相当的方便,优美的string方法加上choice简直是绝配 make_password.py ###简单几行代码执行即可生成记不住的字符串### $ python make_passwd.py DLrw9EiT Qs4Wm84q RQwl4L2L u9g0LgwW jHPtYdyU ... $ python make_passwd.py DLrw9EiT Qs4Wm84q RQwl4L2L u9g0LgwW jHPtYdyU ... 代码如下--注释比

  • 浅析ASP.NET生成随机密码函数

    实现ASP.NET生成随机密码功能是很容易的,下面的代码给出了完整的实现方法: 复制代码 代码如下: publicstaticstringMakePassword(stringpwdchars,intpwdlen) { stringtmpstr=""; intiRandNum; Randomrnd=newRandom(); for(inti=0; i{ iRandNum=rnd.Next(pwdchars.Length); tmpstr+=pwdchars[iRandNum]; } r

  • PHP生成随机密码方法汇总

    使用PHP开发应用程序,尤其是网站程序,常常需要生成随机密码,如用户注册生成随机密码,用户重置密码也需要生成一个随机的密码.随机密码也就是一串固定长度的字符串,这里我收集整理了几种生成随机字符串的方法,以供大家参考. 方法一: 1.在 33 – 126 中生成一个随机整数,如 35, 2.将 35 转换成对应的ASCII码字符,如 35 对应 # 3.重复以上 1.2 步骤 n 次,连接成 n 位的密码 该算法主要用到了两个函数,mt_rand ( int $min , int $max )函数

  • C# 批量生成随机密码必须包含数字和字母并用加密算法加密

    要求: 密码必须包含数字和字母 思路: 1.列出数字和字符. 组成字符串 :chars 2.利用randrom.Next(int i)返回一个小于所指定最大值的非负随机数. 3. 随机取不小于chars长度的随机数a,取字符串chars的第a位字符. 4.循环 8次,得到8位密码 5.循环N次,批量得到密码. 代码实现如下 Main函数: static void Main(string[] args) { string chars = "0123456789ABCDEFGHIJKLMNOPQRS

  • python生成随机密码或随机字符串的方法

    本文实例讲述了python生成随机密码或随机字符串的方法.分享给大家供大家参考.具体实现方法如下: import string,random def makePassword(minlength=5,maxlength=25): length=random.randint(minlength,maxlength) letters=string.ascii_letters+string.digits # alphanumeric, upper and lowercase return ''.joi

  • php生成随机密码的三种方法小结

    使用PHP开发应用程序,尤其是网站程序,常常需要生成随机密码,如用户注册生成随机密码,用户重置密码也需要生成一个随机的密码.随机密码也就是一串固定长度的字符串,这里我收集整理了几种生成随机字符串的方法,以供大家参考. 方法一: 1.在 33 – 126 中生成一个随机整数,如 35, 2.将 35 转换成对应的ASCII码字符,如 35 对应 # 3.重复以上 1.2 步骤 n 次,连接成 n 位的密码 该算法主要用到了两个函数,mt_rand ( int $min , int $max )函数

  • PHP生成随机密码类分享

    类代码: <?php /** * PHP - Password Generator Class * Version 1.0.0 * */ if (@!is_object($passGen) || !isset($passGen)) { $passGen = new Password; } class Password { /** * 大写字母 A-Z * * @var array */ protected $uppercase_chars; /** * 小写字母 a-z * * @var arr

  • Python3实现生成随机密码的方法

    本文实例讲述了Python3实现生成随机密码的方法,在Python程序设计中有着广泛的实用价值.具体方法如下: 本文实例主要实现创建8位随机密码(大小写字母+数字),采用Python3生成了初级算法的随机密码. 主要功能代码如下: __author__ = 'Goopand' import string import random def genPassword(length=8,chars=string.digits+string.ascii_letters): return ''.join(

随机推荐