Speed

Collisions in a Toy Hash

Cut a SHA-256 digest to its first few hex digits and two different messages soon share them; find a collision of a 12-bit toy by listing messages, and see that the full digest is out of reach of such a loop.

The question

A digest is a fixed number of bits, so there are fewer digests than messages: two different messages must exist with the same digest. Such a pair is called a collision. A hash is useful when finding one is hard. How hard is it when the digest is very short?

This lesson cuts the SHA-256 digest to its first three hex digits, 12 bits, and calls the result a toy hash. A 12-bit fingerprint has 4096 possible values, so a collision is easy to find: you only list messages and watch for a repeated value.

Work it by hand

Take the messages m0, m1, m2 and so on: the letter m and a counter. Each has a SHA-256 digest, and the toy hash is the first three hex digits of it.

  • With 4096 possible toy values, any 4097 messages must contain a repeat. That is a counting argument and needs no luck.
  • The repeat usually comes much earlier. The digest of m51 begins d4f500b1 and the digest of m60 begins d4fd6a3b: the first three hex digits d4f are the same, so m51 and m60 collide in the toy hash, though their full digests differ.
  • Written as a loop: hash each message in turn, remember its three digits, and stop at the first message whose digits were seen before.

The loop found its first repeat among 61 messages here. Another counter or another three-digit window gives another number.

Run it

The program lists the messages label followed by 0, 1, 2 and so on and remembers the first width hex digits of each digest in a map. Line 1 makes the map. The loop of lines 4 to 11 stops looking when found is set. Line 7 takes the digits of the message. If the map has them, line 8 returns the earlier and the later message as text, otherwise the digits are stored. The answer is none if no repeat appears among the first 1000 messages.

The label m, 3 hex digits compared, and the two messages of the first collision of the toy hash: m51 and m60.
let seen = str_map();
let answer = "none";
let found = 0;
for (i in range(0, 1000)) {
  if (found == 0) {
    let name = label + str(i);
    let tag = str_substr(ssl.digest("SHA256", name), 0, width);
    if (seen.has(tag)) { answer = seen.get(tag) + " and " + name; found = 1; }
    else { seen.set(tag, name); }
  }
}
{
  "answer": "m51 and m60"
}

You can change width: for the label m, measured, the first repeat comes at message 4, 15, 60 and 156 for 1, 2, 3 and 4 digits, well inside the 1000 messages the loop tries.

With width 0 every message has the same empty prefix and the answer is m0 and m1. For the label m, 5 or more digits give none among the 1000 messages, measured with 5 and with 8; another label can still repeat at 5 digits (the label pw gives pw218 and pw655, measured). Any printable ASCII works as label.

Your turn

The program below should return the counter of the first message that repeats the first digits hex digits of the SHA-256 digest of an earlier message, for the messages stem followed by 0, 1, 2 and so on. It counts from 0 and returns -1 if there is no repeat among the first 1000. digits is from 1 to 4. Read how it finds repeats and judge each result. Check runs it on the data shown and on five others.

The stem doc-, 3 hex digits compared, and the index that the shown program returns, a number from -1 up.
let before = "";
let answer = 0 - 1;
for (i in range(0, 1000)) {
  let tag = str_substr(ssl.digest("SHA256", stem + str(i)), 0, digits);
  if (answer < 0 && tag == before) { answer = i; }
  before = tag;
}
{
  "answer": -1
}

The messages are the text stem followed by the counter 0, 1, 2 and so on up to 999, as text. The toy hash of a message is the first digits hex digits of its SHA-256 digest, with digits from 1 to 4. Return, as an int counting from 0, the counter of the first message whose toy hash equals the toy hash of an earlier message, or -1 if there is none among the 1000 messages. Check runs your program with other stems and other numbers of digits.

Read the result carefully

The loop finds collisions of a toy of 12 bits in a moment because its output is small. The real digest of SHA-256 has 256 bits, and the same loop would not finish on this page or anywhere else in any time that this page can state: the page gives no number of steps and quotes no source for one.

A collision of the toy tells nothing about SHA-256 itself. It shows the counting argument and that a short fingerprint repeats soon. A hash that is used to name or check data needs an output long enough that nobody can list messages until two repeat; what "long enough" means is a question for a standard, not for this toy. The messages are made up.

Recap

  • A digest has fewer values than there are messages, so collisions exist.
  • A toy hash of 12 bits has 4096 values, and listing 4097 messages guarantees a repeat; here m51 and m60 repeat early.
  • The first three hex digits of the SHA-256 digests of m51 and m60 are both d4f.
  • A repeat of a toy shows nothing about the whole digest, which is far beyond such a loop.