addfile#
Feed a filehandle or a file by name into a Digest::SHA object.
The public file-ingestion entry point. Accepts either an open filehandle or a filename string, plus an optional mode character that controls how bytes are read:
"b"(default): raw binary read. Use this for anything that is not human-readable text, or when you need bit-exact file digests."U": universal newlines. When the file looks like text (-T), CR/LF and lone CR line endings are read as LF, so a text file gives the same digest whatever its line endings; any other file is read as in"b"."0": BITS mode. Interpret every'0'and'1'character in the file as a single bit and feed those bits into the hash. Anything else in the file is ignored.
Synopsis#
$sha->addfile("data.bin"); # filename, binary
$sha->addfile($fh); # handle, binary
$sha->addfile("notes.txt", "U"); # universal newlines
$sha->addfile("bits.txt", "0"); # ASCII bit stream
What you get back#
The receiver, for chaining.
Examples#
my $sha = Digest::SHA->new(256);
$sha->addfile("archive.tar.gz");
print $sha->hexdigest;
# Cross-platform digest of a source file
my $sha = Digest::SHA->new(256);
$sha->addfile("src/main.c", "U");
# BITS mode: hash the bit pattern "01011010" repeated
my $sha = Digest::SHA->new(1);
$sha->addfile("bitstream.txt", "0");
my $sha = Digest::SHA->new(256);
open my $fh, "<:raw", "photo.jpg" or die $!;
$sha->addfile($fh);
Edge cases#
A missing or unreadable filename croaks with
Open failed: ...; a read error, such as on a closed filehandle, croaks withRead failed: ....The mode applies only to a filename; a filehandle is read as it is, whatever mode is given.
Unknown mode characters fall back to binary.
Empty files leave the hash state untouched.
Called on something that is not a
Digest::SHAobject, dies withCan't locate object methodorCan't call method.
Differences from upstream#
Fully compatible with upstream Digest::SHA 6.04.
See also#
add- use when the data is already in a Perl scalar.add_bits- for bit-aligned input strings.digest- finalise and return the digest after the file has been consumed.