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" or "p": read through the PerlIO text layer. On platforms that perform CR/LF translation this yields platform-independent digests of text files; on Linux it reads identically to "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($fh, "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

open my $fh, "<", "bitstream.txt" or die $!;
my $sha = Digest::SHA->new(1);
$sha->addfile($fh, "0");
my $sha = Digest::SHA->new(256);
open my $fh, "<:raw", "photo.jpg" or die $!;
$sha->addfile($fh);

Edge cases#

  • Missing or unreadable filename, or a closed filehandle, croaks with Bad filehandle: ....

  • Unknown mode characters fall back to binary.

  • Empty files leave the hash state untouched.

  • Called on a non-Digest::SHA object, returns undef.

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.