Difference between revisions of "Random password generator"

From Scriptwiki
Jump to: navigation, search
m
m
Line 7: Line 7:
 
  ; $pwgen(17,0-9 [[$chr]](36) $+ - $+ $chr(41))
 
  ; $pwgen(17,0-9 [[$chr]](36) $+ - $+ $chr(41))
  
  alias pwgen {
+
  [[alias]] pwgen {
 
   ; first we check if this alias is used as identifier
 
   ; first we check if this alias is used as identifier
   if (![[$isid]]) { return }
+
   if (![[$isid]]) { [[return]] }
 
   ; the next part will fill &chars with characters that can be picked to make the password
 
   ; the next part will fill &chars with characters that can be picked to make the password
 
   ;
 
   ;
 
   ; we cycle the different sets of "used characters"
 
   ; we cycle the different sets of "used characters"
   var %i = $numtok($2,32)
+
   [[var]] %i = [[$numtok]]($2,32)
   while (%i > 0) {
+
   [[while]] (%i > 0) {
     var %set = $gettok($2,%i,32)
+
     var %set = [[$gettok]]($2,%i,32)
 
     ; if the length of the current set is 3, we assume its the right syntax
 
     ; if the length of the current set is 3, we assume its the right syntax
     if ($len(%set) == 3) {
+
     [[if]] ([[$len]](%set) == 3) {
 
     ; get the ascii values of the two characters
 
     ; get the ascii values of the two characters
       var %asc1 = $asc($left(%set,1)) , %asc2 = $asc($right(%set,1))
+
       var %asc1 = [[$asc]]([[$left]](%set,1)) , %asc2 = $asc([[$right]](%set,1))
       var %j = $iif(%asc1 < %asc2,%asc1,%asc2) , %max = $iif(%asc1 < %asc2,%asc2,%asc1)
+
       var %j = [[$iif]](%asc1 < %asc2,%asc1,%asc2) , %max = $iif(%asc1 < %asc2,%asc2,%asc1)
 
       ; we will loop from the smallest ascii number to the largest
 
       ; we will loop from the smallest ascii number to the largest
 
       while (%j <= %max) {
 
       while (%j <= %max) {
 
       ; we add the ascii value to the &chars binvar
 
       ; we add the ascii value to the &chars binvar
         bset &chars $calc($bvar(&chars,0) + 1) %j
+
         [[bset]] &chars [[$calc]]([[$bvar]](&chars,0) + 1) %j
         inc %j
+
         [[inc]] %j
 
       }
 
       }
 
     }
 
     }
     dec %i
+
     [[dec]] %i
 
   }
 
   }
 
   ;
 
   ;
 
   ; now we filled &chars we randomly pick characters out of &chars to make the password
 
   ; now we filled &chars we randomly pick characters out of &chars to make the password
   var %i = $iif($abs($int($1)) > 900,900,$v1)
+
   var %i = $iif([[$abs]]([[$int]]($1)) > 900,900,[[$v1]])
 
   ; we do this $1 times, as long as its not larger then 900
 
   ; we do this $1 times, as long as its not larger then 900
 
   ; (wich is approximate the limit of commands)
 
   ; (wich is approximate the limit of commands)
 
   while (%i > 0) {
 
   while (%i > 0) {
     var %pw = %pw $+ $mid(%chars,$rand(1,$len(%chars)),1)
+
     var %pw = %pw $+ [[$mid]](%chars,$rand(1,$len(%chars)),1)
 
     dec %i
 
     dec %i
 
   }
 
   }

Revision as of 21:34, 5 June 2006

This script will allow you to generate a random password, of required length and using characters you want to.

; Usage: $pwgen(length,used characters)
; Examples
; $pwgen(10,a-z A-Z)
; $pwgen(20,a-z 0-9 0-9)
; $pwgen(17,0-9 $chr(36) $+ - $+ $chr(41))
alias pwgen {
  ; first we check if this alias is used as identifier
  if (!$isid) { return }
  ; the next part will fill &chars with characters that can be picked to make the password
  ;
  ; we cycle the different sets of "used characters"
  var %i = $numtok($2,32)
  while (%i > 0) {
    var %set = $gettok($2,%i,32)
    ; if the length of the current set is 3, we assume its the right syntax
    if ($len(%set) == 3) {
    ; get the ascii values of the two characters
      var %asc1 = $asc($left(%set,1)) , %asc2 = $asc($right(%set,1))
      var %j = $iif(%asc1 < %asc2,%asc1,%asc2) , %max = $iif(%asc1 < %asc2,%asc2,%asc1)
      ; we will loop from the smallest ascii number to the largest
      while (%j <= %max) {
      ; we add the ascii value to the &chars binvar
        bset &chars $calc($bvar(&chars,0) + 1) %j
        inc %j
      }
    }
    dec %i
  }
  ;
  ; now we filled &chars we randomly pick characters out of &chars to make the password
  var %i = $iif($abs($int($1)) > 900,900,$v1)
  ; we do this $1 times, as long as its not larger then 900
  ; (wich is approximate the limit of commands)
  while (%i > 0) {
    var %pw = %pw $+ $mid(%chars,$rand(1,$len(%chars)),1)
    dec %i
  }
  ; and its done
  return %pw
}