class Challah::Random

Random string class, uses ActiveSupport's SecureRandom if possible, otherwise gives a fairly secure random string

Public Class Methods

secure_random?() click to toggle source

Is ActiveSupport::SecureRandom available. If so, we'll use it.

# File lib/challah/random.rb, line 14
def self.secure_random?
  defined?(::SecureRandom)
end
token(length = 30) click to toggle source

Returns a random string for use as a token at the given length.

# File lib/challah/random.rb, line 6
def self.token(length = 30)
  return SecureRandom.hex(length/2) if secure_random?

  c = [(0..9),('a'..'z'),('A'..'Z')].map {|i| i.to_a }.flatten
  (1..length).map{ c[rand(c.length)] }.join
end