Same Password, Same Digest
A bare SHA-256 digest of a password is the same for everyone who chose it, so equal digests reveal equal passwords; compare the digests of three accounts and count how many differ.
The question
A service keeps one SHA-256 digest of the password of each account instead of the password. Two accounts, ann and cy, chose the same password. What can someone who reads the stored digests, and has no list of guesses, still learn from them?
The passwords on this page are test material, made up for the lesson. SHA-256 has no input but the password: the same bytes always give the same digest, for every account.
Work it by hand
Take three accounts with the passwords plum42, lemon7 and plum42. The digest of plum42 is 72672a0e 17094a97 059bff24 4013ac08 71dd7543 847f8076 5038ac54 69b2ecfd. The digest of lemon7 is 47dbf9ba a5104304 77606042 49b2959c c0622ce0 7b5580be f6071348 abdd3f1b.
The first and the third digest are the same, byte for byte, and the second is not. Nobody needs to know either password to see that the first and the third account chose the same one.
Run it
The program reads one account per line, a name, a space and the password, in the box accounts. Line 5 finds the space, so everything after the first space is the password. Line 7 takes it and line 8 hashes it. Line 10 asks whether the digest was seen before, and line 11 remembers it. The picture shows the digests of the first three lines and answer, set on line 3 and raised on line 10, is the number of different digests.

Public test material, generated for this lesson. Never use it for anything real.
let lines = str_split(accounts, "\n");
let seen = str_map();
let answer = 0;
for (i in range(0, lines.len())) {
let sp = str_find(lines.at(i), " ");
if (sp >= 0) {
let pw = str_substr(lines.at(i), sp + 1, str_len(lines.at(i)) - sp - 1);
let d = ssl.digest("SHA256", pw);
lmcode_return_put("d" + str(i), d);
if (seen.has(d) == false) { answer = answer + 1; }
seen.set(d, "1");
}
}
{
"answer": 2,
"d0": "72672a0e17094a97059bff244013ac0871dd7543847f80765038ac5469b2ecfd",
"d1": "47dbf9baa51043047760604249b2959cc0622ce07b5580bef6071348abdd3f1b",
"d2": "72672a0e17094a97059bff244013ac0871dd7543847f80765038ac5469b2ecfd"
}You can change the passwords and the names: printable ASCII and newlines, up to 2048 bytes. The picture reads the first three lines, so keep three lines that each have a space. A line without a space is skipped and has no digest in the picture.
Your turn
The program below is meant to list the names, in list order, of the accounts whose password digest equals the digest of an earlier account. The list users has one account per line, a name, a space and the password, which may contain spaces. The names come back as one text, separated by single spaces, empty if there are none. Read how it compares and judge each result. Check runs it on the data shown and on six others.

Public test material, generated for this lesson. Never use it for anything real.
let lines = str_split(users, "\n");
let before = "";
let found = "";
for (i in range(0, lines.len())) {
let line = lines.at(i);
let sp = str_find(line, " ");
if (sp >= 0) {
let pw = str_substr(line, sp + 1, str_len(line) - sp - 1);
let d = ssl.digest("SHA256", pw);
if (d == before) { found = found + str_substr(line, 0, sp) + " "; }
before = d;
}
}
let answer = str_trim(found);
{
"answer": ""
}users holds one account per line: a name, one space and the password, which may itself contain spaces; a line without a space is ignored. Return, as text, the names of the accounts whose password has the same SHA-256 digest as the password of an earlier line, in list order and separated by single spaces; the text is empty if there are none. Check runs your program with other lists, including an empty one.
Read the result carefully
A Hash Is a Fingerprint shows why the same bytes always give the same digest. Equal digests show equal passwords, and a table of guesses would fit every account at once: one guess, hashed once, is compared with all of them. That follows from the digest having no input but the password. The page does not say how a service should store passwords. A salt and an iteration count are the next steps of this book, and none of the passwords here is a recommendation.
Recap
- A bare digest of a password is the same for every account that chose it.
- Equal digests reveal equal passwords without any guess.
- Equal digests tell you that passwords are equal, not what they are.
- One table of guesses fits every account of a list at once.