3.5.4 Hashing incrementally
The following predicates are provided for building hashes incrementally. This works by first creating a context with crypto_context_new/2, then using this context with crypto_data_context/3 to incrementally obtain further contexts, and finally extract the resulting hash with crypto_context_hash/2.
- [det]crypto_context_new(-Context, +Options)
- Context is unified with the empty context, taking into
account
Options. The context can be used in crypto_data_context/3.
For
Options, see crypto_data_hash/3.
Context is an opaque pure Prolog term that is subject to garbage collection. - [det]crypto_data_context(+Data, +Context0, -Context)
- Context0 is an existing computation context, and Context
is the new context after hashing Data in addition to the
previously hashed data. Context0 may be produced by a prior
invocation of either crypto_context_new/2
or crypto_data_context/3
itself.
This predicate allows a hash to be computed in chunks, which may be important while working with Metalink (RFC 5854), BitTorrent or similar technologies, or simply with big files.
- crypto_context_hash(+Context, -Hash)
- Obtain the hash code of Context. Hash is an atom representing the hash code that is associated with the current state of the computation context Context.
The following hashing predicates work over streams:
- [det]crypto_open_hash_stream(+OrgStream, -HashStream, +Options)
- Open a filter stream on OrgStream that maintains a hash. The
hash can be retrieved at any time using crypto_stream_hash/2.
Available
Options in addition to those of crypto_data_hash/3
are:
- close_parent(+Bool)
- If
true
(default), closing the filter stream also closes the original (parent) stream.
- [det]crypto_stream_hash(+HashStream, -Hash)
- Unify Hash with a hash for the bytes sent to or read from HashStream. Note that the hash is computed on the stream buffers. If the stream is an output stream, it is first flushed and the Digest represents the hash at the current location. If the stream is an input stream the Digest represents the hash of the processed input including the already buffered data.