Sha256: 91dba0e69b860550e20d5fcfbedd615c6c2d3ebc24b463b095176b6f5b6e9d81

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module Authlogic
  # Handles generating random strings. If SecureRandom is installed it will default to this and use it instead. SecureRandom comes with ActiveSupport.
  # So if you are using this in a rails app you should have this library.
  module Random
    extend self
    
    SecureRandom = (defined?(::SecureRandom) && ::SecureRandom) || (defined?(::ActiveSupport::SecureRandom) && ::ActiveSupport::SecureRandom)
    
    if SecureRandom
      def hex_token
        SecureRandom.hex(64)
      end
      
      def friendly_token
        # use base64url as defined by RFC4648
        SecureRandom.base64(15).tr('+/=', '-_ ').strip.delete("\n")
      end
    else
      def hex_token
        Authlogic::CryptoProviders::Sha512.encrypt(Time.zone.now.to_s + (1..10).collect{ rand.to_s }.join)
      end
      
      FRIENDLY_CHARS = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
      
      def friendly_token
        newpass = ""
        1.upto(20) { |i| newpass << FRIENDLY_CHARS[rand(FRIENDLY_CHARS.size-1)] }
        newpass
      end
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kschrader-authlogic-2.1.2 lib/authlogic/random.rb
kschrader-authlogic-2.1.3 lib/authlogic/random.rb