Sha256: f689369d7625850f0fbae9348fbcdc335d67d91b84dddf605f73ba95e6ff35ce

Contents?: true

Size: 582 Bytes

Versions: 50

Compression:

Stored size: 582 Bytes

Contents

module Challah
  # Random string class, uses ActiveSupport's SecureRandom if possible, otherwise gives a fairly
  # secure random string
  class Random
    # Returns a random string for use as a token at the given length.
    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

    # Is ActiveSupport::SecureRandom available. If so, we'll use it.
    def self.secure_random?
      defined?(::SecureRandom)
    end
  end
end

Version data entries

50 entries across 49 versions & 2 rubygems

Version Path
challah-1.6.1 lib/challah/random.rb
challah-1.6.0 lib/challah/random.rb
challah-1.5.0 lib/challah/random.rb
challah-1.4.2 lib/challah/random.rb
challah-1.4.1 lib/challah/random.rb
challah-1.4.0 lib/challah/random.rb
challah-1.3.3 lib/challah/random.rb
challah-1.3.2 lib/challah/random.rb
challah-1.3.1 lib/challah/random.rb
challah-1.3.0 lib/challah/random.rb
challah-1.2.11 lib/challah/random.rb
challah-1.2.10 lib/challah/random.rb
challah-1.2.9 lib/challah/random.rb
challah-1.2.8 lib/challah/random.rb
challah-1.2.7 lib/challah/random.rb
challah-1.2.6 lib/challah/random.rb
challah-1.2.5 lib/challah/random.rb
challah-1.2.5.pre lib/challah/random.rb
challah-1.2.4 lib/challah/random.rb
challah-1.2.3 lib/challah/random.rb