Random numbers and hashes

Two kinds of randomness look alike and are not. The Erlang standard library offers both. A die, a shuffle and a test fixture need a number that is evenly spread and repeatable when the seed is fixed. rand does that. A password reset token, a session id and a salt need a number that an attacker cannot predict even with the source, and crypto does that. This lesson uses both and shows what happens when the wrong one guards a secret.

rand and seeding

rand:uniform/1 answers an integer from 1 to its argument, and rand:uniform/0 a float below 1. The generator keeps its state in the process dictionary, so every process has its own stream. rand:seed/2 fixes the state, which makes a test repeatable:

dice.erl

-module(dice). -export([main/0]). main() -> rand:seed(exsss, {1, 2, 3}), io:format("~p~n", [rand:uniform(6)]), io:format("~p~n", [rand:uniform(6)]), io:format("~p~n", [is_float(rand:uniform())]).

erlc dice.erl

erl -noshell -eval 'dice:main(), halt().'

1 1 true

The two ones are the point: the same seed gives the same sequence on every run, which is what a test needs and what a secret must never have. Without a seed, rand seeds itself from the system at start, so a program that wants a different sequence on every run simply does not call rand:seed.

The predictable token

A token built with rand from a seed is a function of that seed. An attacker who learns one token, or the seed, or the time the program started, can compute every later one. This module seeds the same value twice and gets the same token:

weak.erl

-module(weak). -export([main/0]). main() -> rand:seed(exsss, {7, 7, 7}), First = rand:uniform(1000000), rand:seed(exsss, {7, 7, 7}), Second = rand:uniform(1000000), io:format("~p~n", [First =:= Second]), io:format("~p~n", [First]).

erlc weak.erl

erl -noshell -eval 'weak:main(), halt().'

true 462410

The comparison is true and the token is the same number, which is exactly the property a secret must not have. The generator is fast and good at spreading numbers; it is not a cryptographic generator and does not claim to be.

crypto

The crypto application wraps the cryptographic library of the system. strong_rand_bytes/1 answers bytes from the operating system entropy source, and hash/2 answers a digest of the algorithm named: sha256 gives 32 bytes. The shell shows both, and the token itself is not printed because it changes on every call:

erl

1> byte_size(crypto:strong_rand_bytes(16)). 16 2> binary:encode_hex(crypto:hash(sha256, <<"abc">>)). <<"BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD">> 3> crypto:hash_equals(crypto:hash(sha256, <<"abc">>), crypto:hash(sha256, <<"abc">>)). true

The hash of "abc" is a fixed value, which is why the page can quote it; a token is 16 random bytes, and the program asks for its size instead. A token is base64 or hex text when it leaves the program, and binary:encode_hex/1 is the standard-library way to write it. The modern Erlang lesson shows the sigil that writes the same hex in source.

Comparing secrets

A comparison with =:= stops at the first byte that differs, and the time it takes tells an attacker how much of a guess was right. That is a timing attack, and it is the reason the library ships crypto:hash_equals/2, which compares two binaries in constant time. Use it for a token, a password hash and a signature; use =:= for everything that is not a secret.

A password is never stored as text either: store crypto:hash(sha256, Salt ++ Password) with a random salt per user, or a purpose-built password hash if the project may depend on one. The ETS lesson shows where such a table lives, and the term formats lesson shows how the value is stored without exposing it as text.

Questions

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

  1. What does rand:uniform(6) answer?

    Answer

    An integer from 1 to 6

    The one-argument form answers an integer from 1 to the argument, endpoints included.

  2. Why does a test call rand:seed/2?

    Answer

    To get the same sequence on every run

    A fixed seed makes the generator repeat, which is what a reproducible test needs.

  3. Which call produces a token that an attacker cannot predict?

    Answer

    crypto:strong_rand_bytes(16)

    strong_rand_bytes/1 reads the entropy source of the operating system, not a seeded generator.

  4. Why compare a token with crypto:hash_equals/2?

    Answer

    It compares in constant time, without leaking the matching prefix

    A normal comparison stops at the first difference, and its timing tells an attacker how much was right.

  5. What does this module print? -module(q1). -export([main/0]). main() -> rand:seed(exsss, {1, 2, 3}), io:format("~p~n", [rand:uniform(1)]).

    Answer

    1

    The only integer from 1 to 1 is 1, whatever the seed.

  6. What does this module print? -module(q2). -export([main/0]). main() -> rand:seed(exsss, {1, 2, 3}), io:format("~p~n", [rand:uniform(100)]).

    Answer

    27

    The seeded generator gives a fixed sequence, and the first value of this seed is 27.

  7. What does this module print? -module(q3). -export([main/0]). main() -> io:format("~p~n", [byte_size(crypto:hash(sha256, <<"abc">>))]).

    Answer

    32

    SHA-256 answers a 32-byte digest, whatever the length of the input.

  8. What does this module print? -module(q4). -export([main/0]). main() -> Hash = crypto:hash(sha256, <<"abc">>), io:format("~p~n", [crypto:hash_equals(Hash, Hash)]).

    Answer

    true

    The two binaries are equal, and hash_equals/2 answers a boolean.

  9. What does rand:uniform/0 answer?

    Answer

    A float from 0 up to but not including 1

    The zero-argument form answers a float, which is why the lesson checks is_float/1.

  10. Why is a seeded rand value unsafe as a password reset token?

    Answer

    Anyone who knows or guesses the seed can compute it

    The generator is deterministic once seeded, so the token is a function of a value an attacker may learn.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev