#!C:\Perl\bin\perl.exe ##--------------------------------------------------------------## ## Character Set Generator Module ## ## For Web Manager 2005 ## ## ## ## Author: Marcus G. Tovar ## ## E-mail: to.punisher@gmail.com ## ## Filename: chargen.pm ## ## Format: Windows/OS2 (CR LF) ## ## Created on 01-24-05 @ 06:19:32 GMT -06:00 ## ## ## ## This module is used for generating user id's, and temporary ## ## cookie id's for the database. ## ## ## ## The module recieves arugments passed from the calling ## ## script and generates a set of randomize characters specified ## ## from the argument values and returns the generated set to ## ## the script. This module randomizes digits 0-9, uppercase ## ## characters, and lowercase characters. The returning value ## ## can have any kind of characters in the set that is ## ## available. So you could have up to 6 different combinations ## ## of digits, and upper and lower case characters in one set. ## ## ## ##--------------------------------------------------------------## ## Package The Module ## package chargen; require Exporter; @ISA = qw(Exporter); ## Exporting Sub-routine function for script avaiablilty ## @EXPORT = qw(&randChar); srand; ##-------------------------------------------------------------------- +------------------## ## randChar - The sub-routine will randomize the specified + ## ## characters from the argument values and randomize which + ## ## character to use for the set. Another argument value can be + ## ## passed for the number of characters for the set. + ## ## ## ## Syntax: ## ## ------- ## ## &randChar("[char-types(1,2,3)]-[num-of-chars]-[inc|chars]-[exl|char +s]"); ## ## ## ## char-types - specified in digits. you can have any + ## ## combination of the 3. no spaces or other ## ## splicing allowed. Here other the value options: + ## ## ## ## Argument Value Procedure ## ## -------------- --------- ## ## 1 Will randomize digits 0-9 ## ## 2 Will randomize characters a-z lowercase +## ## 3 Will randomize characters A-Z uppercase +## ## 4 Will randomize unique characters that ## ## are on a basic english keyboard ## ## Example: , . ( ) ! @ # $ etc. ## ## ## ## You can have all three argument values for the char-types. + ## ## so you could have 21 for digits and lowercase characters or + ## ## 123 for all types of available characters for the set. + ## ## ## ## num-of-chars - The value here is the value of the number of + ## ## characters you will have in your set. Having + ## ## a 12 for this value with char-types 1 will +## ## give you 12 randomized digits. ## ## ## ## inc|chars - With this value, ASCII numbers from the value splitt +ed with the bar(|) ## ## character that will be converted to characters from the numb +er, will be ## ## part of the randomize string set. It can be any character fr +om the ## ## ASCII character list, and will ignore the specified char-typ +es to add ## ## that character. This will elimate multiple characters in the + string. ## ## ## ## exl|chars - This value of ASCII number sets splitted by the bar( +|) character, will ## ## exclude characters from the randomize string by the ASCII nu +mber to ## ## character converter. ## ##-------------------------------------------------------------------- +------------------## sub randChar { $#randid = -1; $#tmp = -1; $#inchrs = -1; $#exchrs = -1; $#nonexchr = -1; $#uschrs = -1; $incount = 1; $temp = ""; $quit = 0; my($var) = @_; if(!$var){ $var = "1-1";} @tmp = split(/-/, $var); if(@tmp[2]) { @inchrs = split(/\|/, @tmp[2]); } if(@tmp[3]) { @exchrs = split(/\|/, @tmp[3]); } for($i = 0; $i <= (@tmp[1] - 1); $i++) { $#types = -1; $excount = 0; while($excount == 0) { if(@tmp[0] =~ /1/){ @types[++$#types] = int(rand(10));} if(@tmp[0] =~ /2/) { $temp = int(rand(123)); while($temp < 97){ $temp = int(rand(123));} @types[++$#types] = chr $temp; } if(@tmp[0] =~ /3/) { $temp = int(rand(91)); while($temp < 65){ $temp = int(rand(91));} @types[++$#types] = chr $temp; } if(@tmp[0] =~/4/) { $temp = int(rand(127)); while((($temp < 123 && $temp > 96) || ($temp < 92 && $ +temp > 64)) || (($temp < 58 && $temp > 47) || $temp < 33)){ $temp = int(rand(127));} @types[++$#types] = chr $temp; } if(@exchrs) { $#nonexchr = -1; for($t = 0; $t <= $#types; $t++) { $addt = 1; for($x = 0; $x <= $#exchrs; $x++){ if(@types[$t] eq chr @exchrs[$x]){ $addt = 0;}} if($addt){ @nonexchr[++$#nonexchr] = @types[$t];} } if(!@nonexchr) { $excount = 0; $#types = -1; } else { $#types = -1; $excount = 1; @types = @nonexchr; } } else { $excount = 1; } } if(@inchrs && ($#uschrs < $#inchrs)) { $usechar = 0; for($t = 0; $t <= $#types; $t++) { for($n = 0; $n <= $#inchrs; $n++) { $validchar = 1; if(@uschrs) { for($u = 0; $u <= $#uschrs; $u++) { if(@uschrs[$u] == $n) { $validchar = 0; } } } if($validchar) { if((int(chr @inchrs[$n]) || int(!chr @inchrs[$ +n])) && (int(@types[$t]) || !@types[$t])) { $string = @types[$t]; if(int(chr(127)) == @types[$t]) { return $string; @uschrs[++$#uschrs] = $n; $usechar = 1; $#types = -1; @types[0] = chr @inchrs[$n]; $t = 0; $n = $#inchrs; return @types[0]; exit; } } else { if(chr @inchrs[$n] eq @types[$t]) { return "Digits Do Not Go Here."; @uschrs[++$#uschrs] = $n; $usechar = 1; $#types = -1; @types[0] = chr @inchrs[$n]; $t = 0; $n = $#inchrs; return @types[0]; exit; } } } } } } @randid[$i] = @types[int(rand($#types + 1))]; } return join('', @randid); } 1; ## E-O-F ##
Readmore tags added by davido.
In reply to Re: Found a Perl hole
by NewsToYou!
in thread Found a Perl hole
by NewsToYou!
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |