Random numbers, ids and tokens

Random with a seed

A Random object with a seed produces the same sequence every time it is created with that seed. That is what a test or a simulation wants: a repeatable shuffle, not a surprise.

ruby -e 'r = Random.new(42); puts r.rand(100); r2 = Random.new(42); puts r2.rand(100)'

51
51

The top-level rand uses one shared generator and is seeded from the clock, so two runs differ. The object takes a range (r.rand(1..6)), and shuffle takes the object as its random: argument (names.shuffle(random: r)).

SecureRandom for tokens

A seeded generator is predictable by design, which makes it the wrong tool for a password reset link, an API key or a session id. The securerandom library draws from the operating system and is the one to use for anything a person must not guess:

ruby -e 'require "securerandom"; puts SecureRandom.uuid'

77090d45-73f6-4472-8fce-29fa49018570

ruby -e 'require "securerandom"; puts SecureRandom.hex(8)'

a5250daba79d920f

uuid gives the usual 36-character identifier; hex(8) gives 16 hex characters, one byte per two. urlsafe_base64(16) gives 22 characters that can sit in a URL without escaping.

What to watch for

Seeding a generator for a test is good; seeding it for a token is a vulnerability. If a value protects something, it comes from SecureRandom, never from rand.

A twelve-character hex token is 48 bits, which is plenty for a one-time link and not enough for a long-lived key. Pick the length by what the token protects, and store a hash of it rather than the token itself.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a Random object built with the same seed give?

    Answer

    The same sequence of numbers every time

    The seed decides the whole sequence, which is what a repeatable test needs.

  2. What does this program print? r = Random.new(42) puts r.rand(100) r2 = Random.new(42) puts r2.rand(100)

    Answer

    51 / 51

    Both objects start from 42, so both first draws are 51.

  3. Why do two runs of a program that calls the top-level rand differ?

    Answer

    The shared generator is seeded from the clock

    The default seed changes between runs, unlike an object built with a fixed seed.

  4. Why is a seeded generator the wrong tool for a password reset link?

    Answer

    Its sequence is predictable by design

    A known seed and a known algorithm let a person compute the next value.

  5. From where does SecureRandom draw its bytes?

    Answer

    The operating system

    The library asks the operating system for random bytes, which is what a token needs.

  6. How many characters does SecureRandom.hex(8) produce?

    Answer

    16

    One byte becomes two hex characters, so 8 bytes are 16 characters.

  7. How long is the string from SecureRandom.uuid?

    Answer

    36 characters, with the four dashes

    A UUID is five groups of hex digits joined by dashes, 36 characters in all.

  8. How many characters does SecureRandom.urlsafe_base64(16) produce?

    Answer

    22

    16 bytes become 22 characters without padding, safe in a URL.

  9. How do you shuffle an array with a seeded generator?

    Answer

    names.shuffle(random: r)

    shuffle is an Array method that takes the generator as its random: argument.

  10. A twelve-character hex token is how many bits?

    Answer

    48

    Each hex character is four bits, so twelve are 48.

Article author: Arthur Isaev