Sha256: 4a9c11607f76d231c32b679dc52e1d67036585014f46d4418aac171e5beea706

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 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.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

5 entries across 5 versions & 1 rubygems

Version Path
authlogic-3.8.0 lib/authlogic/random.rb
authlogic-3.7.0 lib/authlogic/random.rb
authlogic-3.6.1 lib/authlogic/random.rb
authlogic-3.6.0 lib/authlogic/random.rb
authlogic-3.5.0 lib/authlogic/random.rb