Score:1

How do most implementations of SHA-256 work?

US flag
user105744

Why is it that most implementations define an sha256_init, sha256_update, and sha256_final function that each operates on a SHA256_CTX object? The specification only mentions returning an almost unique hash for any given string of bits of any given length.

  • What is the reason to have these 3 functions instead of just a standalone function that returns an output hash for an input bitstring?
  • What exactly does CTX stand for?
Maarten Bodewes avatar
in flag
CTX is more implementation specific. Contexts are often used in non-object oriented programming languages to keep state. A similar construction including a context is used for symmetric encryption modes.
forest avatar
vn flag
CTX is a programming term, not necessarily a cryptographic one.
dave_thompson_085 avatar
cn flag
mostly dupe https://crypto.stackexchange.com/questions/52136/cryptographic-hasing-functions-init-update-final
Score:6
ng flag

What is the reason to have these 3 functions instead of just a standalone function that returns an output hash for an input bitstring?

This allows to compute the hash of a piece of information that is made available by segments, rather than as a single memory block. This comes handy in many situations, including

  • When the data is too large to fit memory (e.g. hash of a large file)
  • When the data to hash is the content of many network packets (the alternative would require a memory buffer as large as the sum of the length of the packets, and a copy of each packet into that, which is time consuming and kills the cache).
  • When it's desired to perform activities at time intervals smaller than the duration of the whole hash, in a single-thread context. We break the data into pieces, and perform the activity between the hash of individual pieces.
  • When several hashes must be computed concurrently (e.g. multiple users of a server with less threads than users).

What exactly does CTX stand for?

Context. It holds the current state of the hash being computed, typically the 32 bytes of the current chaining value, a number (typically at most 64) of messages bytes not hashed yet, and the length of the data so far (typically on 8 bytes). A typical SHA-256 context is thus 104 bytes, or slightly more.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.