Method: Waxx#random_string

Defined in:
lib/waxx/waxx.rb

#random_string(size = 32, type = :an, chars = nil) ⇒ Object

Get a pseudo-random (non-cryptographically secure) string to use as a temporary password. If you need real random use SecureRandom.random_bytes(size) or SecureRandom.base64(size).

1. size: Length of string
2. type: [
     any: US keyboard characters
     an:  Alphanumeric (0-9a-zA-Z)
     anl: Alphanumeric lower: (0-9a-z)
     chars: Your own character list
   ]
3. chars: A string of your own characters


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/waxx/waxx.rb', line 59

def random_string(size=32, type=:an, chars=nil)
  if not type.to_sym == :chars
    types = {
      any: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$%^&*()_-+={[}]|:;<,>.?/',
      an: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
      anl: '0123456789abcdefghijklmnopqrstuvwxyz'
    }
    chars = types[type.to_sym].split("")
  end
  opts = chars.size
  1.upto(size).map{chars[rand(opts)]}.join
end