Class: Challah::Random
- Inherits:
-
Object
- Object
- Challah::Random
- Defined in:
- lib/challah/random.rb
Overview
Random string class, uses ActiveSupport’s SecureRandom if possible, otherwise gives a fairly secure random string
Class Method Summary collapse
-
.secure_random? ⇒ Boolean
Is ActiveSupport::SecureRandom available.
-
.token(length = 30) ⇒ Object
Returns a random string for use as a token at the given length.
Class Method Details
.secure_random? ⇒ Boolean
Is ActiveSupport::SecureRandom available. If so, we’ll use it.
14 15 16 |
# File 'lib/challah/random.rb', line 14 def self.secure_random? defined?(::SecureRandom) end |
.token(length = 30) ⇒ Object
Returns a random string for use as a token at the given length.
6 7 8 9 10 11 |
# 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 |