Speed

Digest of abc

SHA-256 turns a message of any length into 32 bytes, always the same for the same bytes; count the bytes of a message and of its digest, and see that a final newline is part of the message.

The question

A hash function reads a message and returns a short fixed-size summary of it, the digest. SHA-256, defined in FIPS 180-4 section 6.2, returns 32 bytes whether the message has three bytes or three million. If you hash the same bytes twice, do you get the same digest, and what exactly counts as "the same bytes"?

The second question matters more than it seems. A message is bytes, not letters, and a text file that ends with a newline is a different message from the same text without it.

Work it by hand

Computing SHA-256 takes 64 rounds of arithmetic on 32-bit words, too long to do by hand, but a published value lets you check a program. RFC 6234 section 8.5, the test driver, lists the digest of the three letters abc:

  • Message: 61 62 63, three bytes.
  • Digest: ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61 f20015ad.
  • Digest length: 32 bytes, which is 64 hex digits, two for each byte.

Now add a newline after the letters. The message becomes 61 62 63 0a, four bytes, and its digest is edeaaff3 f1774ad2 88867377 0c6d6409 7e391bc3 62d7d6fb 34982ddf 0efd18cb. It starts and ends differently, and nothing about the first digest tells you how the second one looks. The digest belongs to the exact bytes.

Run it

The program hashes the text in the box. Line 1 counts the bytes of the text, line 2 asks lmssl for the SHA-256 digest of it, as hex text, and lines 3 and 4 return the two counts for the picture. Line 5 returns the digest. The picture shows the message, its 3 bytes, the digest and its 32 bytes.

The message abc of 3 bytes, and its SHA-256 digest of 32 bytes in hex: ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61 f20015ad.
let n = str_len(text);
let digest = ssl.digest("SHA256", text);
lmcode_return_put("message_bytes", n);
lmcode_return_put("digest_bytes", str_len(digest) / 2);
let answer = digest;
{
  "answer": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
  "message_bytes": 3,
  "digest_bytes": 32
}

You can change the text: the box takes printable ASCII and newlines, up to 2048 bytes. A newline typed at the end of the box is part of the message, and the byte count and the digest change with it. An empty box is accepted and gives a message of 0 bytes with a digest of 32 bytes: the length of the digest does not depend on the message.

Your turn

You do not need a program for this: openssl has a command for it. The command below runs and writes a file out.bin. Read what it writes, decide whether the file holds the 32 bytes of the digest of the message in in/message.bin, and edit the command until it does. You may type any single openssl command. Check runs your command on the data shown and on five others.

The 13 bytes of a message in hex, the first 24 of the 91 bytes that the starting command wrote to out.bin, and the number of bytes in that file, 91.
# in/message.bin holds the bytes of the message.
# The result goes to out.bin.
openssl dgst -sha256 -out out.bin in/message.bin
{
  "answer": "534841322d32353628696e2f6d6573736167652e62696e293d20626130663065376431326561323032343730316639646662636562366233653631376338633637666463663830623830323166613334356663333434643065620a",
  "file": "out.bin",
  "bytes": 91
}

Write one openssl command that writes the SHA-256 digest of the file in/message.bin to out.bin as 32 raw bytes. Check runs your command with other messages in that file: an empty file, a text with a final newline, a text with a letter outside ASCII and longer texts.

Read the result carefully

A digest is not an encryption. Nobody can get the message back from 32 bytes in any way that this page shows, but the digest also hides nothing for a message that can be guessed: that follows in the last lesson of this book. It says nothing about who wrote the message, because anyone can compute it, and it is not a tag: a keyed tag is the subject of another book.

The page shows SHA-256 only, as FIPS 180-4 defines it; the other lessons of this book name other functions and measure them. The messages are made up for the lesson. Letters outside ASCII are bytes too, in an encoding such as UTF-8; the text box of this page takes ASCII only, but the command exercise reads a file of bytes.

Recap

  • SHA-256 turns a message of any length into 32 bytes; the same bytes always give the same digest.
  • The message is bytes: a final newline is part of it and changes the digest.
  • abc has the digest ba7816bf ... f20015ad (RFC 6234 section 8.5); the digest of abc and a newline starts edeaaff3.
  • A digest says nothing about the message but its exact bytes: it is not an encryption and has no key.