For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Random String Generators

Problem this snippet solves:

These procs generate a random string of letters, numbers, or both. Code Source


############################################################

# Note: the RNG used by TCL rand() is not in any way cryptographically secure #

############################################################

How to use this snippet:


Code :

# iRule proc Source - Define the proc named html_encode in a separate iRule named library:

rule library {
proc randomNumberGenerator {length {chars "0123456789"}} {
  set range [expr {[string length $chars]-1}]
  set txt ""
  for {set i 0} {$i < $length} {incr i} {
    set pos [expr {int(rand()*$range)}]
    append txt [string range $chars $pos $pos]
  }
  return $txt
}
proc randomLetterGenerator {length {chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}} {
  set range [expr {[string length $chars]-1}]
  set txt ""
  for {set i 0} {$i < $length} {incr i} {
    set pos [expr {int(rand()*$range)}]
    append txt [string range $chars $pos $pos]
  }
  return $txt
}
proc randomNumberLetterGenerator {length {chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"}} {
  set range [expr {[string length $chars]-1}]
  set txt ""
  for {set i 0} {$i < $length} {incr i} {
    set pos [expr {int(rand()*$range)}]
    append txt [string range $chars $pos $pos]
  }
  return $txt
}
}

# Call the procedure from another iRule using the name of the iRule where the proc is defined as the namespace and then the name of the procedure (library::html_encode):

# iRule that calls the string generating proc:
when CLIENT_ACCEPTED {
  set x [call library::randomNumberGenerator 10]
  set y [call library::randomLetterGenerator 10]
  set z [call library::randomNumberLetterGenerator 10]
  log local0. "Random Strings: $x / $y / $z"
}
# log output: Random Strings: 8648734163 / vmQSXmqgxB / Xx7mKZ7733
Published Mar 18, 2015
Version 1.0