One Letter Changes Everything
Change one letter of a message and its SHA-256 digest changes in about half of its bits; count the differing bits of two digests, and see that about half is a measurement and not a law.
The question
Two messages differ in one letter: abc and abd. Their digests have the same length, 32 bytes. How much do the two digests have in common? If a hash were a checksum that changes only a little when the message changes a little, the digests would look alike. A hash is built to do the opposite.
Count the bits in which the two digests differ. A digest has 256 bits, and two unrelated bit strings of that length agree in about half of their positions.
Work it by hand
The digests of abc and abd (the first is in RFC 6234 section 8.5, the second computed by the program below) are:
- abc: ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61 f20015ad
- abd: a52d159f 262b2c6d db724a61 840befc3 6eb30c88 877a4030 b65cbe86 298449c9
Compare the first byte: ba is 1011 1010 and a5 is 1010 0101. Five of the eight bits differ, the fourth to the eighth counted from the left. Do this for all 32 bytes and add the counts. Half of 256 would be 128. The program below finds 122: close to half, but not exactly half. "About half" is a measurement of this pair, not a rule that every pair obeys.
Run it
The program counts the differing bits of the SHA-256 digests of first and second. Lines 1 and 2 compute the two digests. The outer loop of lines 4 to 10 takes the 32 bytes one by one, and the inner loop of lines 7 to 9 takes their 8 bits with bits_of, shown under the program, and counts those that differ. Line 13 returns the count.

let d1 = ssl.digest("SHA256", first);
let d2 = ssl.digest("SHA256", second);
let bits = 0;
for (i in range(0, 32)) {
let a = hex_u8(d1, i);
let b = hex_u8(d2, i);
for (k in range(0, 8)) {
if (bits_of(a, k, 1) != bits_of(b, k, 1)) { bits = bits + 1; }
}
}
lmcode_return_put("first_digest", d1);
lmcode_return_put("second_digest", d2);
let answer = bits;
{
"answer": 122,
"first_digest": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
"second_digest": "a52d159f262b2c6ddb724a61840befc36eb30c88877a4030b65cbe86298449c9"
}The picture marks the bytes that differ; for this pair that is every byte. You can change both texts: printable ASCII and newlines, up to 2048 bytes each. Equal texts give 0 differing bits, and an empty text is accepted: the digest of the empty message exists.
Your turn
The program below should return in how many of the 64 hex digits the SHA-256 digests of two texts m1 and m2 differ, comparing the digit at each position of the first digest with the digit at the same position of the second. Read how it compares, run it on pairs of texts, and judge each count. Check runs it on the pair shown and on five others.

let d1 = ssl.digest("SHA256", m1);
let d2 = ssl.digest("SHA256", m2);
let differ = 0;
for (i in range(0, 63)) {
if (str_substr(d1, i, 1) != str_substr(d2, i, 1)) { differ = differ + 1; }
}
let answer = differ;
{
"answer": 60
}Return, as an int, the number of hex digits in which the SHA-256 digest of the text m1 and the SHA-256 digest of the text m2 differ, counting the positions 1 to 64 where the digit of the first digest is not the digit of the second at the same position. Check runs your program on this pair and on other pairs of texts, one of them with an empty text.
Read the result carefully
The count of 122 differing bits belongs to the pair abc and abd. Other pairs give other counts, and no count is promised: the page measures, it does not prove anything about all pairs. The page measures a few pairs; it does not show that no pair of close messages can be told apart by their digests.
A digest that changes completely does not detect which letter changed, and does not say whether the change was made by a person or by an accident. Only a keyed tag, in another book, says who made a digest. The texts are made up and the digest is SHA-256 as FIPS 180-4 defines it.
Recap
- A one-letter change of the message gives a digest that looks unrelated to the old one.
- For
abcandabd, 122 of the 256 bits differ: about half, not exactly half. - Counting bits and counting hex digits are different measures: a digest has 256 bits and 64 hex digits.
- A digest by itself does not say who computed it.