context#

Save or restore the raw internal MD5 state — for advanced use only.

Synopsis#

my @state = $ctx->context;                       # get
$ctx->context($blocks, $state_bytes, $partial);  # restore

What you get back#

Called with no extra arguments, returns a list that fully captures the current digest state:

  1. $blocks — number of complete 64-byte blocks processed so far, as an unsigned integer.

  2. $state_bytes — 16 raw bytes holding the four MD5 state words in little-endian order.

  3. $partial — present only when some bytes have been added that do not yet fill a full block. Up to 63 bytes of unprocessed data.

Called with those same three values, restores the context to the captured state and returns the object itself. This is the only way to persist a digest across process boundaries — serialise the three values, hand them off, then restore on the other side and continue adding.

For almost every use case, clone is a cleaner tool. Reach for context only when the state needs to survive something clone cannot cross, such as a database round-trip or an out-of-band storage step.

Examples#

Round-trip a partial digest through storage:

my $ctx = Digest::MD5->new;
$ctx->add($first_half);
my @saved = $ctx->context;            # freeze state

## ... later, possibly in another process ...

my $ctx = Digest::MD5->new;
$ctx->context(@saved);                # thaw state
$ctx->add($second_half);
print $ctx->hexdigest;                # same as hashing whole thing

Serialise for storage:

use Storable qw(freeze thaw);
my $blob = freeze([$ctx->context]);   # three values in, one blob out

## ...

my $ctx = Digest::MD5->new;
$ctx->context(@{ thaw($blob) });

Edge cases#

  • The third ($partial) value is only returned when length($partial) > 0. Be prepared for a two-element list when the input fits exactly into whole blocks.

  • Passing a $state_bytes argument shorter than 16 bytes silently leaves the state words untouched. Do not rely on this — pass exactly what context gave you.

  • context is the only method whose interface is genuinely raw. Its output is sensitive to the algorithm, not to a documented wire format; do not pretend values produced by one digest class are interchangeable with another.

Differences from upstream#

Fully compatible with upstream Digest::MD5 2.58.

See also#

  • clone — the usual way to snapshot state in-process

  • digest — finalise rather than persist

  • new — fresh context for use with context as a restore target

  • add — continue hashing after a restore