reset#
Rewind a Digest::SHA object, optionally switching algorithm.
Clears buffered data and restores the initial hash state so the same
object can be reused for a fresh digest. With no argument the algorithm
is preserved; with an algorithm argument the object is rewound and
retargeted at that algorithm. Accepts the same algorithm shapes as new
(integer code or string like "SHA-256").
Synopsis#
$sha->reset; # rewind, keep algorithm
$sha->reset(512); # rewind and switch to SHA-512
$sha->reset("SHA-384"); # string form
What you get back#
The receiver on success (chainable), or undef if an algorithm argument
was supplied that could not be parsed.
Examples#
my $sha = Digest::SHA->new(256);
$sha->add("first");
my $a = $sha->hexdigest; # finalises and auto-resets already
$sha->reset; # defensive rewind before reuse
$sha->add("second");
my $b = $sha->hexdigest;
my $sha = Digest::SHA->new(256);
$sha->reset(512)->add("hello");
print $sha->algorithm; # 512
my $sha = Digest::SHA->new(256);
my $ok = $sha->reset("bogus"); # undef
Edge cases#
Called on a non-
Digest::SHAobject, returnsundef.After the built-in auto-reset inside
digest, callingresetexplicitly is harmless.
Differences from upstream#
Fully compatible with upstream Digest::SHA 6.04.
See also#
new— when called on an instance, functionally equivalent toreset.digest— auto-rewinds after finalisation, so explicitresetis only needed when you abandon a computation partway.clone— if you want a copy of the pre-reset state, clone first.